📅  最后修改于: 2020-10-25 11:06:31             🧑  作者: Mango
Object.freeze()方法冻结一个对象,以防止向其添加新属性。此方法防止修改现有属性,属性和值。
Object.freeze(obj)
对象:要冻结的对象。
此方法返回传递给函数的对象。
Chrome | 45.0 |
Edge | 12.0 |
Firefox | 32.0 |
Opera | No |
const object1 = {
property1: 22
};
const object2 = Object.freeze(object1);
object2.property1 = 33;
// Throws an error in strict mode
console.log(object2.property1);
输出:
22
const obj1 = { property1: 'freeze'};
const obj2 = Object.freeze(obj1);
obj2.property1 = 'new_data';
console.log(obj2.property1);
输出:
" freeze "
var obj = { prop: function() {}, name: 'charry' };
console.log(obj);
obj.name = 'karri';
delete obj.prop;
console.log(obj);
var o = Object.freeze(obj);
obj.name = 'chris';
console.log(obj);
输出:
[object Object] {
name: "charry",
prop: function() {}
}
[object Object] {
name: "karri"
}
[object Object] {
name: "karri"
}