[js] 防篡改对象
#preventExtensions
对象成员可修改、可删除,不可增加
var person = { name: "Nicholas" }; console.log(Object.isExtensible(person)); //true Object.preventExtensions(person); console.log(Object.isExtensible(person)); //false person.age = 29; console.log(person.age);//undefined person.name = 'husky'; console.log(person.name);//husky delete person.name; console.log(person.name);//undefined,删除了
#seal
可修改,不可增加、不可删除
var person = { name: "QM" }; console.log(Object.isExtensible(person)); //true console.log(Object.isSealed(person)); //false Object.seal(person); console.log(Object.isExtensible(person)); //false console.log(Object.isSealed(person)); //true person.age = 29; console.log(person.age); //undefined person.name = 'husky'; console.log(person.name);//husky delete person.name; console.log(person.name); //husky
#freeze
不可增加、不可修改、不可删除
var person = { name: "Nicholas" }; console.log(Object.isExtensible(person)); //true console.log(Object.isSealed(person)); //false console.log(Object.isFrozen(person)); //false Object.freeze(person); console.log(Object.isExtensible(person)); //false console.log(Object.isSealed(person)); //true console.log(Object.isFrozen(person)); //true person.age = 29; console.log(person.age); //undefined delete person.name; console.log(person.name); //"Nicholas" person.name = "Greg"; console.log(person.name); //"Nicholas"