1. Object.freeze
const obj1 = {
a:1,
};
const obj2 = Object.freeze(obj1);
obj1 === obj2;
// 返回原来的对象
Object.isExtensible(obj2); // false
Object.getOwnPropertyDescriptor(obj2,'a');
// {value: 1, writable: false, enumerable: true, configurable: false}
Object.freeze做了两件事情,
(1)给对象设置,Object.preventExtension(obj1),
禁止更改原型,禁止添加属性,
(2)为对象的每一个属性设置,writable:false,
禁止更改属性值,
(3)为对象的每一个属性设置,configurable:false。
禁止删除属性,禁止更改writable为true,禁止更改enumerable为false,禁止更改configuable为true
注:
禁止添加属性,是Object.preventExtensions控制的,
而禁止删除属性,是configuable:false控制的。
例子,
// 禁止更改原型
Object.setPrototypeOf(obj2,{c:3});
// 非严格模式:Uncaught TypeError: #<Object> is not extensible
// 严格模式:Uncaught TypeError: #<Object> is not extensible
// 禁止添加属性
obj2.b = 2;
// 非严格模式,静默失败,obj2还是{a:1}
// 严格模式:Uncaught TypeError: Cannot add property b, object is not extensible
// 禁止更改属性值
obj2.a = 2;
// 非严格模式,静默失败,obj2还是{a:1}
// 严格模式:Uncaught TypeError: Cannot assign to read only property 'a' of object '#<Object>'
// 禁止删除已有属性
delete obj2.a;
// 非严格模式,返回false
// 严格模式:Uncaught TypeError: Cannot delete property 'a' of #<Object>
/// 禁止更改writable为true
Object.defineProperty(obj2,'a',{
writable:true,
});
// 非严格模式:Uncaught TypeError: Cannot redefine property: a
// 严格模式:Uncaught TypeError: Cannot redefine property: a
// 禁止更改enumerable为false
Object.defineProperty(obj2,'a',{
enumerable:false,
});
// 非严格模式:Uncaught TypeError: Cannot redefine property: a
// 严格模式:Uncaught TypeError: Cannot redefine property: a
// 禁止更改configuable为true
Object.defineProperty(obj2,'a',{
configuable:true,
});
// 非严格模式,静默失败,configuable还是false
// 严格模式:静默失败,configuable还是false
注:
在严格模式下,更改configurable为true,同样是静默失败。
2. Object.seal
const obj1 = {
a:1,
};
const obj2 = Object.seal(obj1);
obj1 === obj2;
// 返回原来的对象
Object.isExtensible(obj2); // false
Object.getOwnPropertyDescriptor(obj2,'a');
// {value: 1, writable: true, enumerable: true, configurable: false}
Object.freeze做了两件事情,
(1)给对象设置,Object.preventExtension(obj1),
禁止更改原型,禁止添加属性,
(2)为对象的每一个属性设置,configurable:false,
禁止更改属性值,
与Object.freeze不同的是,Object.seal后的对象是可写的writable:true。
参考
MDN: Object.freeze
MDN: Object.preventExtensions
MDN: Object.isExtensible
MDN: Object.getOwnPropertyDescriptor
MDN: Object.seal