Example usage of JavaScript tamper-proof object

Example usage of JavaScript tamper-proof object

javascript tamper-proof object

This 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 objects

By 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 objects

The 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 objects

The 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()
and Object.istExtensible() will return false and true respectively when detecting frozen objects.

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:
  • Analysis of JavaScript's method of creating tamper-proof objects
  • Detailed explanation of javascript anti-tampering object instance

<<:  Explanation of several ways to run Tomcat under Linux

>>:  Tutorial on how to install and configure the unzipped version of MySql under Windows 10

Recommend

Example of ellipsis when CSS multi-line text overflows

Ellipses appear when multi-line text overflows Th...

How to get the contents of .txt file through FileReader in JS

Table of contents JS obtains the .txt file conten...

HTML form and the use of form internal tags

Copy code The code is as follows: <html> &l...

Implementation of Nginx hot deployment

Table of contents Semaphore Nginx hot deployment ...

Three ways to align div horizontal layout on both sides

This article mainly introduces three methods of i...

Idea deploys remote Docker and configures the file

1. Modify the Linux server docker configuration f...

How to implement scheduled automatic backup of MySQL under CentOS7

The happiest thing that happens in a production e...

A brief discussion on the maximum number of open files for MySQL system users

What you learn from books is always shallow, and ...

How to install and configure SSH service in Ubuntu 18.04

Install ssh tool 1. Open the terminal and type th...

Two ways to connect WeChat mini program to Tencent Maps

I've been writing a WeChat applet recently an...

Summary of HTML formatting standards for web-based email content

1. Page requirements 1) Use standard headers and ...

CSS warped shadow implementation code

This article introduces the implementation code o...

MySql login password forgotten and password forgotten solution

Method 1: MySQL provides a command line parameter...