What if I don’t want people to add or remove elements from an OBJ object during development?
The description of this property can be modified and deleted from the corresponding object only if the 64X key is set to true, exercising any additional control system that disables any additional element You can still add new objects to new objects
Look at the code example
Object.defineProperty(obj,"prop", {configurable:false.// The second speed symbol of the property can be modified only if the property is true, and it can be deleted from the corresponding object
enumerable:false.// Enumerable appears in the object's enumeration property only if it is true
writable:false.// Indicates whether the value of an attribute can be modified
value:"" // The value of this property can be any valid JS value (value, object, function)
})
// The prop property becomes a property that cannot be deleted, cannot be modified, cannot be enumerated, cannot be modified
// Sample code
const obj = {name:"East east"}
Object.defineProperty(obj,"name", {writable:false.configurable:false
})
/ / modify
obj.name = "Acquisition of Tencent";
console.log(obj.name); / / east east
// Add new attributes
obj.address = "Qingdao"
console.log(obj.address); / / Qingdao
Copy the code
Object.freeze(person);
Frozen objects are neither extensible nor sealed, and the [[Writable]] property of the object’s data property is set to false. Accessor properties are still writable if the [[Set]] function is defined. The object.freeze () method defined in ES5 can be used to freeze objects.
The code is shown in
var person = {
name: "Rhubarb"
};
Object.freeze(person); // Freeze the object
person.age = 18; // Adding a new age attribute does not work
console.log(person.age); //undefined
delete person.name; // Delete the name attribute
console.log(person.name); / / "rhubarb"
person.name = "Mimi"; // Changing existing attributes didn't work
console.log(person.name); / / "rhubarb"
The object.isfrozen () method is used to detect frozen objects. Because frozen objects are both sealed and unexpandable,
So detecting frozen objects with Object.isextensible () and object.issealed () returns false and true, respectively.
var person = {
name: "Wang"
};
alert(Object.isExtensible(person)); //true
alert(Object.isSealed(person)); //false
alert(Object.isFrozen(person)); //false
Object.freeze(person);
alert(Object.isExtensible(person)); //false
alert(Object.isSealed(person)); //true
alert(Object.isFrozen(person)); //true
Copy the code
Object.seal()
This method closed (seal) an object stop adding new properties And all the properties of the tag for configuration The current value of the attribute As long as the original writable Can change an object is scalable (you can add new attributes) 1 sealing of an object will make this object can not add new properties And all of the existing properties will become Unconfigurable (the effect of a property being unconfigurable is that the property cannot be deleted) 2 and a data property cannot be redefined as an accessor property or vice versa but the value of the property can still be changed. 3 Try to delete the property of a sealed object or convert the property of a sealed object from a data property to an accessor property TypeError is silently raised as a result
Look at the sample code
var person = {
name: "Acquisition of Baidu"
}
Object.seal(person); // Close the object
person.age = 19;
console.log(person.age); //undefined
// Cannot add attributes to the sealed object
delete person.name;
console.log(person.name);// Acquire Baidu
// Attributes in the sealed object cannot be modified.
// use the object.issealed () method to determine whether the Object isSealed and, since the sealed Object is not extensible,
// So object.isextensible () also returns false.
var person = {
name: "Wang"
}
Object.seal(person);
console.log(Object.isSealed(person)); //true
console.log(Object.isExtensible(person)); //false
Copy the code
Tamper-proof object for ES5
Note, however, that once an object is defined as tamper-proof, it cannot be undone. By default, all objects are extensible. That is, you can add attributes and methods to an object at any time
/ / Object. PreventExtensions () method
//1 Change this behavior without adding attributes or methods.
//2 This object cannot be extended, but it does not affect the original properties, which can still be modified or deleted.
var person = {
name: "Acquisition of Tencent"
}
Object.preventExtensions(person); // Make person unextensible
person.money = 14;
console.log(person); //{name: "Tencent acquisition "}
console.log(person.money); //undefined
person.name = "Buy Ali";
console.log(person.name); // Buy Ali
// use the object.isextensible () method to determine whether the Object isExtensible.
var p = {
name: "Wang"
}
console.log(Object.isExtensible(p)); //ture
Object.preventExtensions(p); // Set p to unextensible
console.log(Object.isExtensible(p)); //false
Copy the code
Es6 constant freezing
// Constant freeze
const esObj = { // Here is an object
name:"es6".year:2015
}
//Object.freeze(esObj); {name:"es6",year:2015}
esObj.name ="es2015";
console.log(esObj) //{name:"es2015", year:2015}
const arr = ['es6'.'es7'.'es8']; // This is an array
/ / Object. Freeze (arr) / / freeze
arr[0] = 'es2015'
console.log(arr) //["es2015","es7","es8"]
/* The above code shows that the contents of the heap memory of objects and arrays can be changed because of the address of the reference stored in the stack
const esObj ={
name:"es6".year:'2015'.extension: ["es7"."es8"."es9"]}Object.freeze(esObj);
esObj.extension[0] = "es2016"
console.log(esObj)
/* {name: "es6", year: "2015", extension: Array(3)} extension: Array(3) 0: "es2016" 1: "es8" 2: "es9" */
Freeze is only a shallow level of the internal array or will change this!!
// Let's wrap the function to define myFreeze
function myFreeze(obj){
Object.freeze(obj);
Object.keys(Obj).forEach(function(key){ The // object.keys () method returns an array of the current Object properties
if(typeof obj[key] === "object") {// If the attribute corresponding to key is an object
myFreeze(obj[key]) // Then freeze the recursion again}})}2 / / method
function meFreeze(obj){
// Check whether the parameter is object
if(obj instanceof Object) {for(let key in obj){
if(obj.hasOwnProperty(key)){
Object.defineProperty(obj,key,{
writable:false.// Read-only attribute
})
Object.seal(obj) // Enclose the object}}}return obj
}
Copy the code
Note: In the tamper-proof object or frozen object, in non-strict mode and strict mode, throw the result is different. If there is any error or not strict place, please leave a comment, thank you very much, to the author is also a kind of encouragement.