从冻结的父对象创建新对象

问题描述 投票:0回答:5

此示例创建一个对象,冻结它,然后从冻结的对象创建一个新对象。 如果第二个对象尝试更改测试属性,则它不能。它仍然冻结着 第一个对象的值为 10。

//Create an object and freeze it

var first = {
    test: 10
};
Object.freeze(first);

//Create a second object from the first one and
//try and change the new test property (you can't)

var second = Object.create(first);
second.test = 20;
console.log(second.test); //10

这是我的问题:

second.test
是新对象上的新属性,还是只是对冻结的第一个对象中的属性的引用?
是否可以使用冻结的
first.test
作为默认值,但如果需要的话让
second.test
覆盖它?

我问这个问题的原因是因为我想创建一个不可变的基础对象作为具有默认值的模板,然后使用它来创建我可以自定义的新对象。最好的方法是什么?

谢谢!

javascript inheritance prototypal-inheritance
5个回答
16
投票

second
实际上是一个新对象,
first
second
的原型。原因是

second.test = 20;

不起作用是因为在分配时,它将查找原型上的设置(即

configurable
enumerable
writable
[[Extensible]]
),并且如果其中任何一个为 false,则不会分配给实例1 。要直接分配给实例,您必须在
Object.defineProperty
上使用
second
:

var first = {
    test: 10
};
Object.freeze(first);

var second = Object.create(first);
Object.defineProperty(second, 'test', { value: 20, enumerable: true, configurable: true, writable: true });
console.log(second.test); // 20

1:

[[Put]]
:ECMAScript 规范,§8.12.5


11
投票

使用对象.分配

         var first = {
            test: 10
        };
        Object.freeze(first);

        //Create a second object from the first one and
        //try and change the new test property (you can't)

        var second = Object.assign({}, first, {
            test: 20
        });
        console.log(second.test); //20

2
投票

在您的情况下,

second
是对
first
的引用(就像您假设的那样)。解决方案是克隆您的对象。没有构建克隆对象的方法 - 你必须自己做,这里是如何(源)

function clone(obj){
   if(obj == null || typeof(obj) != 'object')
      return obj;

   var temp = obj.constructor();

   for(var key in obj)
       temp[key] = clone(obj[key]);
   return temp;
}

然后你这样使用它:

var first = {
    test: 10
};
Object.freeze(first);

// clone it into a new one
var second = clone(first);
second.test = 20;
console.log(second.test); // 20 where the first is locked

1
投票

在新版本的 javascript 中,您可以使用对象解构来创建具有旧对象属性的新对象。

const first = {
    test: 10
};
Object.freeze(first);

//Clone the object
const second = {...first};
second.test = 20;
console.log(second.test); // 20 where the first is locked

0
投票

这个问题在 2022 年通过引入 structedClone 函数得到解决:

const first = { a : 10 };
Object.freeze(first);
const second = structuredClone(first);
second.a = 20;
console.log(first, second);

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.