![](/img/trans.png)
[英]Why doing Object.create(null) and later setting its prototype to Object.prototype will not allow to execute methods in Object.prototype
[英]Why does setting an object to be not extensible make its [[prototype]] immutable?
MDN - Object.preventExtensions页面说
该方法使目标的
[[prototype]]
不可变; 任何[[prototype]]
重新分配都会抛出TypeError
。 此行为特定于内部[[prototype]]
属性,目标 object 的其他属性将保持可变。
我的问题是:
为什么将 object 设置为不可扩展使其 [[prototype]] 不可变? ( Object.preventExtensions()
, Object.seal()
, Object.freeze()
等)
If the internal [[prototype]]
wasn't made an immutable property, you would be able to circumvent Object.preventExtensions()
by exchanging the internal [[prototype]]
of the object with another value using Object.setPrototypeOf()
, effectively将新值的所有属性添加到 object:
let a = {}; // a now has all the properties of Array.prototype Object.setPrototypeOf(a, Array.prototype); a.push('foo'); console.log(a); let b = Object.preventExtensions({}); // must not be able add properties to b in the same way Object.setPrototypeOf(b, Array.prototype); b.push('bar'); console.log(b);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.