JS prohibits object attribute extension, sealing objects and freezing objects

Object constant properties

Concept: set the writable and configurable attributes to false;

 

//Set the writable and configurable of the property to false
var damu={};
Object.defineProperty(damu,"wife",{
    value:"fbb"
})
Object.defineProperty(damu,"wife",{
    value:"fbb2"
});
//Cannot change, cannot delete. Can add
//console.log(damu);// Cannot redefine property

damu.wife="damu"
delete damu.wife;
console.log(damu);//{wife: "fbb"}

damu.wife2="fbb2";
console.log(damu);//{wife2: "fbb2", wife: "fbb"}

 

 

Prohibit attribute extension

concept
If an object can add new attributes, the object is extensible, making the object non extensible, that is, there can be no new attributes; Since attribute descriptors are the management of attributes, if you want to prohibit object extension, you can't use attribute descriptors to control, but you need to call the methods of other objects.
There are two methods of object attribute extension:

 

Object.isExtensible method: object The isextensible () method determines whether an object is extensible (whether new attributes can be added to it).

 

Syntax: object isExtensible(obj)
Parameter: obj the object to be detected;

By default, objects created are extensible by default

 

//New objects are extensible by default, regardless of how they are created
//The literal method is used
var empty = {a:1};
console.log(Object.isExtensible(empty) === true);//true

//Equivalent to using attribute descriptors
empty = Object.create({},{
    "a":{
        value : 1,
        configurable : true,//Configurable
        enumerable : true,//enumerable 
        writable : true//Writable
    }
});
console.log(Object.isExtensible(empty) === true);//true

//How attributes are created
var damu={};
Object.defineProperty(damu,"wife",{
    value:"fbb"
})
console.log(Object.isExtensible(damu) === true);//true

Object.preventExtensions method: this method makes an object non extensible, that is, it can never add new properties and return the original object.

Syntax: object preventExtensions(obj);
Parameter: obj object to become non extensible;
var damu = {};
Object.defineProperty(damu,'wife',{
    value:'lbb'
});
console.log(damu);//{wife: "lbb"}
Object.preventExtensions(damu);
//  damu.age = 18;
//  console.log(damu);//{wife: "lbb"}

 (function fail(){
       "use strict";
       damu.d = "4";//throws a TypeError
   })();
console.log(damu);//Cannot add property d, object is not extensible

Description:

If an object can add new properties, the object is extensible. preventExtensions can make this object non extensible, that is, there can be no new attributes.
It should be noted that the properties of non extensible objects can usually still be deleted.
An attempt to add a new property to a non extensible object will fail, but it may fail silently (by default) or throw a TypeError exception (strict mode).
Note: object Preventextensions can only prevent an object from adding new self attributes. You can still add attributes to the prototype of the object.

 

Seal object

Concept:
Sealed objects are those that are not extensible and whose properties are not configurable.
In other words, sealed objects refer to those objects that cannot add new attributes, delete existing attributes, and modify the enumerability, configurability and Writeability of existing attributes, but may modify the value of existing attributes.
Available methods: prohibit Object.preventExtensions(obj); On the basis of, adjust the configurable of the existing attributes to false;

 

//The setting object is not extensible and configurable
var damu = {};
Object.defineProperty(damu,'wife',{
    value:'lbb'
    //At this time, the default configurable and writable are false.
});
console.log(damu);//{wife: "lbb"}
Object.preventExtensions(damu);

damu.age = 18;
delete damu.wife;//{wife: "lbb"}
console.log(damu);//{wife: "lbb"};
//Proof is not configurable and extensible. However, this setting process is very troublesome. Try to use a simple object method to set the sealed object.

Two methods about sealing properties

Object.isSealed method: this method determines whether an object is sealed.

 

Syntax: object isSealed(obj);
Parameter: obj the object to be detected;

Description: returns true if the object is sealed; otherwise, returns false.

 

//Use object If preventextensions is set to non extensible and then non configurable, it will become sealed.
var damu = {};
Object.defineProperty(damu,'wife',{
    value:'lbb'
    //At this time, the default configurable and writable are false.
});
Object.preventExtensions(damu);
console.log(Object.isSealed(damu));//true

Object.seal() method: you can seal an object and return the sealed object.

Syntax: object seal(obj)
Parameter: obj object to be sealed
var damu = {};
Object.defineProperty(damu,'wife',{
    value:'lbb'
    //At this time, the default configurable and writable are false.
});
console.log(Object.getOwnPropertyDescriptor(damu,"wife"));//{value: "lbb", writable: false, enumerable: false, configurable: false}
Object.seal(damu);
console.log(Object.isSealed(damu));//true
//Silent failure
damu.d = 'd';
delete damu.wife;
console.log(damu);//{wife: "lbb"}

console.log(Object.getOwnPropertyDescriptor(damu,"wife"))

Description:

Usually, an object is extensible (you can add new properties).

Sealing an object will make it impossible to add new properties, and all existing properties will become unconfigurable.
The effect of non configurable attribute is that the attribute cannot be deleted, and a data attribute cannot be redefined as an accessor attribute, or vice versa. However, the value of the property can still be modified.
An attempt to delete a property of a sealed object or convert a property of a sealed object from a data property to an accessor property will silently fail or throw a TypeError exception (strict mode).
Properties inherited from the prototype chain are not affected. However, the value of the proto () attribute cannot be modified.

 

Freeze object

Concept:
An object is frozen, which means that it is not extensible, all properties are non configurable, and all data properties are non writable;
Or frozen objects refer to those objects that cannot add new attributes, modify the value of existing attributes, delete existing attributes, and modify the enumerability, configurability and Writeability of existing attributes. In other words, the object is always immutable;
Available methods: Based on the sealed object (Object.seal(obj)), adjust the writable of existing attributes to false:

Guangzhou brand design companyhttps://www.houdianzi.com PPT template downloadhttps://redbox.wode007.com

//Based on the sealed object (Object.seal(obj)), adjust the writable of existing attributes to false
var damu = {};
Object.defineProperty(damu,'wife',{
    value:'lbb',
    //writable:true
});
//seal up
Object.seal(damu);
//Anti expansion
Object.preventExtensions(damu);
damu.age = 18;
delete damu.wife;
damu.wife = 'zdy';
console.log(damu);//{wife: "lbb"};

Two methods about sealing properties

Object.isFrozen method: this method determines whether an object is frozen.

 

Syntax: object isFrozen(obj)
Parameter: obj detected object

Object.freeze() method: you can freeze an object.

 

Syntax: object freeze(obj);
Parameter: obj object to be frozen;
var damu = {wife:'lbb'};
console.log(Object.getOwnPropertyDescriptor(damu,"wife"));
//{value: "lbb", writable: true, enumerable: true, configurable: true}
//Freeze object
Object.freeze(damu);
console.log(Object.getOwnPropertyDescriptor(damu,"wife"));
//{value: "lbb", writable: false, enumerable: true, configurable: false}
damu.age = 18;
delete damu.wife;
damu.wife = 'zdy';
console.log(damu);//{wife: "lbb"}

Description:

All of the frozen object's own properties cannot be modified in any way.
Any attempt to modify the object will fail, either silently or by throwing an exception (in strict mode).
The value of the data attribute cannot be changed, and so can the accessor attribute (getter and setter) (but because it is a function call, it gives the illusion that this attribute can still be modified).
If the value of a property is an object, the property in the object can be modified unless it is also a frozen object.

//Shallow deformation
var damu={wifes:{wife1:"fbb",wife2:"lyf",wife3:"zdy"}};
damu.wifes.wife1="lbb";
console.log(damu);//wifes:{wife1: "lbb", wife2: "lyf", wife3: "zdy"}

//Deep freezing
var damu={
    wifes:{wife1:"fbb",wife2:"lyf",wife3:"zdy"}
};
Object.freeze(damu);
//Traverse and call the freezing method to freeze all the objects in the object properties.
for(item in damu){
    Object.freeze(damu[item]);
}
damu.wifes.wife1="lbb";
console.log(damu);//wifes:{wife1: "fbb", wife2: "lyf", wife3: "zdy"}

Tags: Javascript

Posted by joozt on Wed, 04 May 2022 11:14:45 +0300