04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Object Lock - Down Methods<br />

Chapter 22: The Evolution of JavaScript<br />

One of the major downsides of ECMAScript 3 was the inability to prevent an object from being<br />

augmented, which allowed developers to overwrite native functionality and introduced the possibility<br />

of malicious code being run on a page. ECMAScript 3.1 seeks to remedy this situation by providing three<br />

different levels of object lock - down: non - extensible, sealed, and frozen.<br />

An object is non - extensible when properties cannot be added to it. You can make an object non -<br />

extensible by calling Object.preventExtensions() and passing in the object to change. Doing so sets<br />

the internal [[Extensible]] attribute to false , ensuring that no further properties can be added. To<br />

detect if an object is extensible, you can use Object.isExtensible() as in the following example:<br />

var person = {<br />

name: “Nicholas”,<br />

age: 29<br />

};<br />

alert(Object.isExtensible(person));<br />

Object.preventExtensions(person);<br />

alert(Object.isExtensible(person));<br />

person.job = “Software Engineer”;<br />

//true<br />

//false<br />

//ERROR!<br />

This example makes the person object non - extensible so that no further properties may be added to the<br />

instance. Note, however, that new properties may be added to the object ’ s prototype and inherited from<br />

it unless the prototype is non - extensible.<br />

The second type of lock - down is a sealed object. An object is said to be sealed when its properties cannot<br />

be added or deleted and property attributes cannot be changed using Object.defineProperty() . You<br />

can seal an object by using the Object.seal() method, which performs two operations. First, it sets the<br />

[[Flexible]] attribute on each property to false , ensuring that it cannot be deleted or modified.<br />

Second, it sets the [[Extensible]] attribute on the object to false , preventing any further properties<br />

from being added to the object. A sealed object is also non - extensible, so Object.isExtensible() will<br />

return false for a sealed object, as in the following example:<br />

var person = {<br />

name: “Nicholas”,<br />

age: 29<br />

};<br />

alert(Object.isSealed(person)); //false<br />

Object.seal(person);<br />

alert(Object.isSealed(person)); //true<br />

alert(Object.isExtensible(person)); //false<br />

alert(delete person.name); //false<br />

alert(person.name); “Nicholas”<br />

In this example, the person object is sealed. Because it is sealed, it is also non - extensible, so<br />

Object.isExtensible() returns false . When the delete operator is applied to person.name ,<br />

it returns false because the property cannot be deleted. Accessing person.name afterwards yields the<br />

same initial value.<br />

The last lock - down mode is called frozen. An object is said to be frozen when it cannot have properties<br />

added or deleted, property attributes cannot be changed, and all properties are read-only. Calling<br />

Object.freeze() on an object sets its [[Extensible]] attribute to false and sets the [[Writable]]<br />

747

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!