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

d13*_*d13 16 javascript inheritance prototypal-inheritance

此示例创建一个对象,冻结它,然后从冻结对象创建一个新对象.如果第二个对象尝试更改测试属性,则不能.它保持冻结,第一个对象的值为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
Run Code Online (Sandbox Code Playgroud)

这是我的问题:

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

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

谢谢!

Qan*_*avy 13

second实际上是一个新的对象,first作为原型second.之所以

second.test = 20;
Run Code Online (Sandbox Code Playgroud)

不起作用是因为在分配时,它将寻找原型上的设置(即 configurable,enumerable,writable,[[Extensible]]),而不是分配给背景.如果这些都是假的1.要直接分配给背景,你将不得不使用Object.definePropertysecond:

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
Run Code Online (Sandbox Code Playgroud)

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


Gor*_*lla 9

使用Object.assign

         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
Run Code Online (Sandbox Code Playgroud)

  • 非常外部的情况,但请注意,如果冻结对象包含对其他冻结对象的引用,并且您尝试覆盖 _their_ 属性,则这仍然不起作用。对于简单的数据结构仍然是一种非常有用的技术,只是没有人认为这是创建不可变的一种万无一失的方法。 (2认同)