防篡改对象(不可扩展对象、密封对象、冻结对象)

不可扩展对象:不能添加

var person = {name: 'wangdanting'};
Object.preventExtensions(person);

person.age = 23;
console.log(person.age); // => undefined

person.name = 'abc';
console.log(person.name); // => 'abc'

console.log(Object.isExtensible(person)); // => false

密封对象:不能添加、不能删除

var person = {name: 'wangdanting'};
Object.seal(person);

person.age = 23;
console.log(person.age); // => undefined

person.name = 'abc';
console.log(person.name); // => 'abc'

delete person.name
console.log(person.name); // => 'abc'

console.log(Object.isSealed(person)); // => true

冻结对象:不能添加、不能删除、不能修改

var person = {name: 'wangdanting'};
Object.freeze(person);

person.age = 23;
console.log(person.age); // => undefined

person.name = 'abc';
console.log(person.name); // => 'wangdanting'

delete person.name
console.log(person.name); // => 'wangdanting'

console.log(Object.isExtensible(person)); // => false
console.log(Object.isSealed(person)); // => true

 

你可能感兴趣的:(防篡改对象(不可扩展对象、密封对象、冻结对象))