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.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
Run Code Online (Sandbox Code Playgroud)
1 :: [[Put]]
ECMAScript规范,§8.12.5
使用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)
归档时间: |
|
查看次数: |
3810 次 |
最近记录: |