
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
The Almighty Prototype Chain
|
305
The __proto__ Property
We saw earlier in this chapter that the instanceof operator tells us whether an
instance belongs to a particular class. But how does the interpreter know an individ-
ual object’s class? Or, more specifically, how does the interpreter know which con-
structor’s
prototype to access when searching for an object property? While that kind
of internal information normally is hidden in object-oriented languages, Action-
Script exposes it to the developer.
When any object is created, the interpreter automatically assigns it a special prop-
erty called
__proto__ (note the two underscores on either side of the word proto).
Then, into
__proto__, the interpreter copies a reference to the object’s class’s
prototype property. For example, when we create an instance of Book called asGuide,
asGuide.__proto__ is set to Book.prototype:
var asGuide = new Book("Colin Moock", "A book about ActionScript");
trace(asGuide.__proto__ = = Book.prototype); // Displays: true
Primarily, the __proto__ property is used internally by the ActionScript interpreter.
However, we can use it to determine the class of an object (as shown earlier in
Example 12-5) or to access overridden properties. Although
__proto__ is writable, it
should not be tampered with in most situations. ...