This article describes JavaScript tamper-proof object. Share it for your reference, as follows:
Objects in JavaScript have multiple built-in properties Configurable, Writable, Enumerable, Value, Get and Set to control the behavior of properties. The same ES5 also has several methods to specify the behavior of the object. We know that objects in javascript can be shared and can be expanded by default:
//Once the object is set to be tamper-proof, it cannot be revoked//As we all know, general objects can be expanded at willvar person = {name:'liufang'}; = 22; (+"<br>");//22
We can add, delete or modify other attributes or methods. However, this can also lead to some problems, such as when multiple people develop, some attributes are artificially modified, causing engineering trouble. This prompted the birth of tamper-proof objects.There are three levels of tamper-proof objects, namely non-expandable objects, sealed objects and frozen objects.
Unexpandable objects
Let’s talk about unexpandable objects first. You can invalidate the newly added attributes by setting the normal object to an unexpandable object:
//Default targets(person);//Set as anti-expansion object = 'ff'; (+"<br>");//undefined //Note that new attributes cannot be added//Detect whether it is an expandable object((person)+"<br>");//false //Although the anti-expansion object cannot add attributes, the attributes can be deleteddelete ; (+"<br>");//undefined , has been deleted successfully
It can be seen that although the non-expandable object can prevent new attributes from being added, it cannot prevent others from deleting attributes, and of course it cannot prevent modifications. Therefore, the second level of sealed objects were introduced.
Sealed objects
Sealing objects is to add a rule on the basis of non-expandable objects, that is, attributes cannot be deleted.
//Seal the object//It cannot be deleted on the basis of non-expansionvar people = {name:'liufang'}; (people);//Seal the objectdelete ; (+"<br>");//liufang, it means that it cannot be deleted = "tyq";//Although it cannot be deleted, it can be modified(+"<br>");//tyq, indicating that the modification is successful//Detection((people)+"<br>");//true
It can be seen that although sealing objects prevents deletion, they still cannot prevent modification, so there is a high-level restriction, that is, freezing objects.
Freeze the object
The frozen object cannot be expanded, deleted or modified.
//Frozen the object//Cannot expand, delete, modifyvar man = {name:'tyq'}; (man); //Detection((man));//true
The last thing to note is that once the object is set to put the tampered object, it cannot be revoked, so it needs to be carefully considered.
For more information about JavaScript, please view the topic of this site: "JavaScript object-oriented tutorial》、《Summary of json operation skills in JavaScript》、《Summary of JavaScript switching effects and techniques》、《Summary of JavaScript search algorithm skills》、《Summary of JavaScript animation special effects and techniques》、《Summary of JavaScript Errors and Debugging Skills》、《Summary of JavaScript data structure and algorithm techniques》、《JavaScript traversal algorithm and skills summary"and"Summary of JavaScript mathematical operations usage》
I hope this article will be helpful to everyone's JavaScript programming.