23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

• Third, a data descriptor or accessor descriptor object. Data descriptors may<br />

contain the following four members. Minimally, a data descriptor must contain a<br />

value or writable member.<br />

• value contains the member’s value, which defaults to undefined.<br />

• writable contains a boolean indicating whether the member’s value is<br />

writable. true means that it is, and false, which is the default, means that it<br />

is not. So, false means that you cannot assign a new value to a member<br />

with the = operator.<br />

• configurable contains a boolean indicating whether a member may be<br />

deleted and whether its descriptor attributes are writable, with the<br />

exception of writable, which is carved in stone. false, the default, means<br />

that a member may not be deleted and that its configurable and<br />

enumerable attributes may not be changed. true means the inverse.<br />

CHAPTER 6 ■ FUNCTIONS AND ARRAYS<br />

• enumerable contains a boolean indicating whether the member would be<br />

enumerated in a for in loop. true means that it would be, and false, the<br />

default, means that it would not. So, enumerable provides a way to ensure a<br />

member such as constructor doesn’t appear in a for in loop.<br />

Note that the writable, configurable, and enumerable descriptor attributes default to false. On the<br />

other hand, for a member traditionally created or modified with the = operator, writable, configurable,<br />

and enumerable default to true. That’s your clue to continue using = for most assignment operations.<br />

Note that, if you add a member with the =, you may later change its writable, configurable, and<br />

enumerable attributes with Object.defineProperty().<br />

■ Note I will not cover accessor descriptors in this book. You use them to provide a function that is called<br />

whenever a property value is accessed or set.<br />

For browsers that do not implement Object.defineProperty(), we’d simply want to assign the<br />

descriptor’s value member with the = operator and disregard the writable, configurable, and<br />

enumerable members. Note that, if the descriptor just has a writable member, descriptor.value<br />

evaluates to undefined.<br />

if (Object.defineProperty === undefined) {<br />

Object.defineProperty = function (obj, name, descriptor) {<br />

obj[name] = descriptor.value;<br />

};<br />

}<br />

if (Object.defineProperties === undefined) {<br />

Object.defineProperties = function () {<br />

};<br />

}<br />

if (Object.create === undefined) {<br />

Object.create = function () {<br />

};<br />

}<br />

187

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

Saved successfully!

Ooh no, something went wrong!