HTML and CSS Reference
In-Depth Information
var myCircle = circle.create(3);
assertEquals(6, myCircle.diameter);
// circle is not a function
assertException(function () {
assertFalse(myCircle instanceof circle);
This example creates a single object that exposes a create method to construct
the new object. Thus there is no need for new or prototype , and prototypal
inheritance works as expected. An interesting side effect of this style is that you can
call myCircle.create(radius) to create circles that inherit from myCircle
and so on.
This is just one possible way to implement inheritance without constructors in
JavaScript. Regardless of what you think of this particular implementation, I think
the example clearly shows why constructors and the new keyword are unneeded in
JavaScript, particularly in ES5, which provides better tools for working with objects
and prototypal inheritance.
8.2.5 Reserved Keywords as Property Identifiers
In ES5, reserved keywords can be used as object property identifiers. This is par-
ticularly important in the light of the added native JSON support, because it means
that JSON is not restricted in available property names. Reserved keywords as im-
plemented in ES3 caused trouble, for instance when implementing the DOM, as
Listing 8.15 shows an example of.
Listing 8.15 Reserved keywords and property identifiers
// ES3
element.className; // HTML attribute is "class"
// HTML attribute is "for"
// Is valid ES5
Search WWH ::

Custom Search