javascript tamper-proof objectThis thing is rarely used, and I personally feel it is not very useful. However, it can be used as a tool to show off, haha, let's get started. . 1. Non-extensible objectsBy default, objects are extensible, which means that properties and methods can be added to them at any time. Now you can change this behavior using the Object.preventExtensions(object) method so that you can no longer add properties and methods to the object. For example: var person={name : 'jack'}; Object.preventExtensions(person); person.age=13; console.log(person.age);///undefine Although you cannot add new members to the object, the existing members are not affected at all, and you can still modify and delete your existing members. In addition, you can use the isExtensible() method to determine whether the object is extensible. For example: var person={name : 'jack'}; alert(Object.isExtensible(person)); //true Object.preventExtensions(person); alert(Object.istExtensible(person)); //false 2. Sealed objectsThe second protection level defined by ECMAScript 5 for objects is sealed objects. Use the Object.seal(object) method to change an object to a sealed object. Sealed objects are not extensible and the [[configurable]] attribute of existing members will be set to false. This means that properties and methods cannot be deleted, because you cannot use Object.defineProperty() to modify data to access its properties, or vice versa. However, the attribute value can be modified. var person = {name:'tom'}; Object.seal(person); person.age=12; console.log(person.age);//undefine delete person.name; console.log(person.name);//tom person.name="jack"; alert(person.name);//jack Use the Object.isSealed() method to determine whether an object is sealed. Because sealed objects are not extensible, using Object.istExtensible() to detect sealed objects will also return false (i.e., not extensible). var person = {name:'tom'}; alert(Object.isExtensible(person)); ///true, extensible alert(Object.isSealed(person)); ////false, unencrypted Object.seal(person); alert(Object.isExtensible(person)); ///false, not extensible alert(Object.isSealed(person)); ////true, already encrypted 3. Frozen objectsThe most stringent level of tamper prevention is frozen objects. Frozen objects are neither extensible nor sealed, and the [[Writable]] attribute of the object's data properties will be set to false. If a set function is defined, the accessor property is still writable. Now you can use the Object.freeze(object) method to change the object to a frozen object. var person={name : 'tony'}; Object.freeze(person); person.age=12; alert(person.age);//undefine delete person.name; alert(person.name);//tony person.name = 'jack'; alert(person.name);//tony Use the Object.isFrozen() method to detect whether an object is a frozen object. Because frozen objects are both non-extensible objects and sealed objects, use isExtensible() var person = {name:'tom'}; alert(Object.isExtensible(person));///true, extensible alert(Object.isSealed(person));////false, unencrypted alert(Object.isFrozen(person));////false, unencrypted Object.seal(person); alert(Object.isExtensible(person)); ///false, not extensible alert(Object.isSealed(person)); ////true, already encrypted alert(Object.isFrozen(person)); ////true, already frozen The above is the detailed content of the usage example of JavaScript tamper-proof object. For more information about JavaScript tamper-proof object, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Explanation of several ways to run Tomcat under Linux
>>: Tutorial on how to install and configure the unzipped version of MySql under Windows 10
Ellipses appear when multi-line text overflows Th...
Table of contents JS obtains the .txt file conten...
Copy code The code is as follows: <html> &l...
Table of contents Semaphore Nginx hot deployment ...
This article mainly introduces three methods of i...
1. Modify the Linux server docker configuration f...
Inserting Data insert into table name (column nam...
When optimizing a website, we must learn to use e...
The happiest thing that happens in a production e...
What you learn from books is always shallow, and ...
Install ssh tool 1. Open the terminal and type th...
I've been writing a WeChat applet recently an...
1. Page requirements 1) Use standard headers and ...
This article introduces the implementation code o...
Method 1: MySQL provides a command line parameter...