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

Detailed explanation of the execution process of JavaScript engine V8

Table of contents 1. V8 Source 2. V8 Service Targ...

JavaScript function detailed introduction

Any number of statements can be encapsulated thro...

Detailed explanation of the middleman mode of Angular components

Table of contents 1. Middleman Model 2. Examples ...

Background gradient animation effect made by css3

Achieve results Implementation Code html <h1 c...

Detailed process of configuring Https certificate under Nginx

1. The difference between Http and Https HTTP: It...

Exploration of three underlying mechanisms of React global state management

Table of contents Preface props context state Sum...

Installation tutorial of MySQL 5.7.17 zip package version under win10

The installation tutorial of mysql5.7.17 is share...

Summary of the use of TypeScript in React projects

Preface This article will focus on the use of Typ...

Example code for implementing background blur effect with CSS

Is it the effect below? If so, please continue re...

Summary of tips for setting the maximum number of connections in MySQL

Method 1: Command line modification We only need ...

How to reset MySQL root password under Windows

Today I found that WordPress could not connect to...

Seven Principles of a Skilled Designer (1): Font Design

Well, you may be a design guru, or maybe that'...

MySQL 5.7.31 64-bit free installation version tutorial diagram

1. Download Download address: https://dev.mysql.c...

Vue achieves seamless carousel effect

This article shares the specific code of Vue to a...