關鍵字:seal, freeze, property descriptor。
1、Object.seal()
參考文檔(2)中這樣描述:
The
Object.seal()
method seals an object, preventing new properties from being added to itand marking all existing properties as non-configurable.
Values of present properties can still be changed as long as they are writable.
主要就是兩點:
- 阻止添加新屬性;
- 現有屬性變得non-configurable.
第一點好理解;第二點需要搞清楚non-configurable是什么意思,configurable在這篇博文中有詳細解釋,
這里再貼一遍:
當configurable設為false時,
1、不可以通過delete去刪除該屬性從而重新定義屬性;
2、不可以轉化為訪問器屬性;
3、configurable和enumerable不可被修改;
4、writable可單向修改為false,但不可以由false改為true;
5、value是否可修改根據writable而定。
理解了configurable的意思,也就能理解為啥會有這個句話了:被封的對象仍可能可以修改對象的屬性值。
Values of present properties can still be changed as long as they are writable.
2、Object.freeze()
參考文檔(3)中這樣描述:
The
Object.freeze()
method freezes an object. A frozen object can no longer be changed;freezing an object prevents new properties from being added to it,
existing properties from being removed,
prevents changing the enumerability, configurability, or writability of existing properties,
and prevents the values of existing properties from being changed.
In addition, freezing an object also prevents its prototype from being changed.
此函數“冰凍”對象本身以及一切現有的屬性值(value)以及屬性的特性(property descriptor)。
在函數Object.seal()中也許還可以修改屬性值以及修改 屬性的特性writable(true-->false),
但是在Object.freeze()中,這些都干不了。
參考文檔:
(1)https://www.cnblogs.com/tlz888/p/10389532.html
(2)https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal
(3)https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze