Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement tamper-proof objects with js

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces js how to achieve tamper-proof objects, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Tamper-proof object

Any object in Javascript can be modified by code running in the same environment, so developers sometimes need to define tamper-proof objects (tamper-proof object) to protect themselves.

Non-extensible object

All objects are extensible by default (add properties and methods)

Let person = {name: 'addone'}; person.age = 20

The second behavior person object extends the age property, and of course you can block this behavior by using Object.preventExtensions ()

Let person = {name: 'addone'}; Object.preventExtensions (person); person.age = 20 person.age / / undefined

You can also use Object.isExtensible () to determine whether an object is extensible.

Let person = {name: 'addone'}; Object.isExtensible (person); / / trueObject.preventExtensions (person); Object.isExtensible (person); / / false

Please remember that this is not extensible, that is, you cannot add properties or methods.

Sealed object

Sealed objects are not extensible, and properties and methods cannot be deleted

Let person = {name: 'addone'}; Object.seal (person); person.age = 20 delete person.name;person.age / / undefinedperson.name / / addone

On the other hand, there is Object.isSealed () to judge whether it is sealed or not.

Let person = {name: 'addone'}; Object.isExtensible (person); / / trueObject.isSealed (person); / / falseObject.seal (person); Object.isExtensible (person); / / falseObject.isSealed (person); / / true

Frozen object

This is the most stringent tamper-proof level. Frozen objects are not scalable, sealed, and cannot be modified.

Let person = {name: 'addone'}; Object.freeze (person); person.age = 20 addone delete person.name;person.name =' addtwo'person.age / / undefinedperson.name / / addone

There is also Object.isFrozen to detect.

Let person = {name: 'addone'}; Object.isExtensible (person); / / trueObject.isSealed (person); / / falseObject.isFrozen (person); / / falseObject.freeze (person); Object.isExtensible (person); / / falseObject.isSealed (person); / / trueObject.isFrozen (person) / / true Thank you for reading this article carefully. I hope the article "how to achieve tamper-proof objects in js" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report