Search within TutorialsPark

Javascript Functions : Tamper Proof Object

Learn to create Objects which cannot be Tampered

What is Tamper Proof Objects?

In JavaScript, every object can be manipulated by codes executing within the same context, but at times this could result in complications, this issue can be addressed using Tamper-proof Object.

The most important thing about Tamper-proof Object is that once the operation is performed it cannot be reversed.

Javascript Tamper Proof: Nonextensible Objects.

In JavaScript, objects can be extended by adding more properties and methods as and when desired.

But this behaviour can be altered by making use of the preventExtensions() method, which ensures that no properties and methods are added to the object.

Even though no new properties or methods could be added to the, the exisiting properties and methods can be modified or deleted using the method isExtensible().

Example: Javascript Functions: Non Extensible.

Give it a TRY! » Note: To execute the above script, the Browser JavaScript Interpreter must support ECMAScript 5.

JavaScript Tamper Proof : Sealed Objects

Another advanced way to create Tamper Proof Object is by creating what is called a " sealed object " .

A Sealed Object cannot be extended and existing object members have their [[Configurable]] attribute set as false.

What this does is, you cannot delete properties and methods as data properties cannot be altered to accessor properties, or vice versa via Object.defineProperty()

To seal an object make use of the method Object.seal(), to see if an object is sealed or not use the method Object.isSealed()

Example: JavaScript Function : Sealed Objects

Give it a TRY! » Note: Sealed objects return false for Object.isExtensible() .

Javascript Tamper Proof : Frozen Objects

The most strictest form of tamper-proof object is called as a frozen object , these objects are both non extensible as well as sealed

Objects of this type have their data properties attribute [[Writable]] set as false , hence accessor properties can still be written to them , but only if a [[Set]] function has been defined.

The objects can be frozen by using the method Object.freeze(), to detect is the objects are frozen or not use the method Object.isFrozen()

Example: JavaScript Tamper Proof : Frozen Object

Give it a TRY! » Note: Frozen objects are both sealed and extensible, hence will return false for isExtensible() and true for isSealed()