HTML and CSS Reference
In-Depth Information
8.2 Updates to the Object Model
Of all the updates in ECMAScript 5, I find the updated object model to be the
most exciting. As we discussed in Chapter 7, Objects and Prototypal Inheritance,
JavaScript objects are simple mutable collections of properties, and even though
ES3 defines attributes to control whether properties can be overwritten, deleted,
and enumerated, they remain strictly internal, and thus cannot be harnessed by client
objects. This means that objects that are dependent on their (public and mutable)
properties need to employ more error checking than desired to remain reasonably
robust.
8.2.1 Property Attributes
ES5 allows user-defined property descriptors to overwrite any of the following
attributes for a given property.
enumerable — Internal name [[Enumerable]], formerly [[DontEnum]],
controls whether the property is enumerated in for-in loops
configurable — Internal name [[Configurable]], formerly
[[DontDelete]], controls whether the property can be deleted with delete
writable — Internal name [[Writable]], formerly [[ReadOnly]], controls
whether the property can be overwritten
get — Internal name [[Get]], a function that computes the return value of
property access
set — Internal name [[Set]], a function that is called with the assigned value
when the property is assigned to
In ES5 we can set a property in two ways. The old school way, shown in
Listing 8.1, in which we simply assign a value to a property, or the new way, shown
in Listing 8.2.
Listing 8.1 Simple name/value assignment
var circle = {};
circle.radius = 4;
 
 
Search WWH ::




Custom Search