HTML and CSS Reference
In-Depth Information
}
});
circle.radius = 4;
assertEquals(8, circle.diameter);
circle.diameter = 3;
assertEquals(3, circle.diameter);
assertEquals(1.5, circle.radius);
assertException(function () {
circle.diameter = {};
});
}
8.2.4 Making Use of Property Attributes
Using the new property attributes makes it possible to create much more sophis-
ticated programs with JavaScript. As we already saw, we can now create properly
immutable objects. Additionally, we can now also emulate how the DOM works,
by providing property accessors that have logic behind them.
Previously, I also argued that Object.create (backed by Object.
defineProperty ) will obliterate the need for object constructors along with
the instanceof operator. In particular, the example given in Listing 8.7 cre-
ates an object with which the instanceof operator will only make sense with
Object . However, using Object.create does not mean we cannot have a usable
instanceof operator. Listing 8.11 shows an example in which Object.create
is used inside a constructor to provide a meld between ES3 and ES5 style prototypal
inheritance.
Listing 8.11 Constructor using Object.create
function Circle(radius) {
var _radius;
var circle = Object.create(Circle.prototype, {
radius: {
configurable: false,
enumerable: true,
set: function (r) {
 
Search WWH ::




Custom Search