简体   繁体   English

为什么将 object 设置为不可扩展使其 [[prototype]] 不可变?

[英]Why does setting an object to be not extensible make its [[prototype]] immutable?

The MDN - Object.preventExtensions pages said MDN - Object.preventExtensions页面说

This method makes the [[prototype]] of the target immutable;该方法使目标的[[prototype]]不可变; any [[prototype]] re-assignment will throw a TypeError .任何[[prototype]]重新分配都会抛出TypeError This behavior is specific to the internal [[prototype]] property, other properties of the target object will remain mutable.此行为特定于内部[[prototype]]属性,目标 object 的其他属性将保持可变。

And my question is:我的问题是:

Why does setting an object to be not extensible make its [[prototype]] immutable?为什么将 object 设置为不可扩展使其 [[prototype]] 不可变? ( Object.preventExtensions() , Object.seal() , Object.freeze() etc.) ( 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 adding all the properties on the new value to the object: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM