JavaScript tamper-proof object
This thing is rarely used, and I personally feel it is not very useful, but it can be used as a weapon to show off, haha, let's do it. .
1. Unscalable Objects
By default, all objects can be extended, that is, properties and methods can be added to the object at any time. Now use the (object) method to change this behavior so that you can no longer add properties and methods to the object. For example:
var person={name : 'jack'}; (person); =13; ();///undefine
Although you cannot add new members to the object, existing members are not affected at all. You can still modify and delete your existing members. In addition, using the isExtensible() method can also determine whether the object can be extended. For example:
var person={name : 'jack'}; alert((person));//true (person); alert((person));//false
2. Sealed objects
The second level of protection defined for an object is to seal the object. Use the (object) method to change the object to a sealed object. The sealed object is not extensible, and the [[configurable]] attribute of existing members will be set to false. This means that attributes and methods cannot be deleted, because () cannot be used to modify the data to access its attributes, or vice versa. But the attribute value can be modified.
var person = {name:'tom'}; (person); =12; ();//undefine delete ; ();//tom ="jack"; alert();//jack
Use the() method to determine whether the object is sealed, because the sealed object is also not extensible, so using() to detect that the sealed object will also return false (that is, not extensible)
var person = {name:'tom'}; alert((person));///true, extensiblealert((person));/////false, not encrypted (person); alert((person));///false, not extensiblealert((person));/////true, already encrypted
3. Freeze object
The strictest level of tampering is to freeze objects. The frozen objects are neither extensible nor sealed. Moreover, the [[Writable]] property of the object's data attribute will be set to false. If the set function is defined, the accessor attribute is still writable. Now use the (object) method to change the object to a frozen object.
var person={name : 'tony'}; (person); =12; alert();//undefine delete ; alert();//tony = 'jack'; alert();//tony
Use the() method to detect whether the object is a frozen object. Because the frozen object is both an unscalable object and a sealed object, it isExtensible() is used.
and () detect the frozen object will return false and true respectively.
var person = {name:'tom'}; alert((person));///true, extensiblealert((person));/////false, not encryptedalert((person));/////false, not encrypted (person); alert((person));///false, not extensiblealert((person));/////true, already encryptedalert((person));/////true, it has been frozen
The above is the detailed content of the usage example of JavaScript tamper-proof objects. For more information about JavaScript tamper-proof objects, please pay attention to my other related articles!