簡體   English   中英

為什么將 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM