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
Table of contents 1. V8 Source 2. V8 Service Targ...
Any number of statements can be encapsulated thro...
There are also two servers: Preparation: Set the ...
Table of contents 1. Middleman Model 2. Examples ...
Achieve results Implementation Code html <h1 c...
1. The difference between Http and Https HTTP: It...
Table of contents Preface props context state Sum...
The installation tutorial of mysql5.7.17 is share...
Preface This article will focus on the use of Typ...
Is it the effect below? If so, please continue re...
Method 1: Command line modification We only need ...
Today I found that WordPress could not connect to...
Well, you may be a design guru, or maybe that'...
1. Download Download address: https://dev.mysql.c...
This article shares the specific code of Vue to a...