AS1 OOP: Controlling OOP
by senocular
__proto__
The __proto__ property is a property in all objects which is
a reference to the next inherited object in an object’s
inheritance chain. It’s the __proto__ reference that lets an
object know what methods and properties are being directly
shared with it. Like constructor, __proto__ is an automatic
property that is added for you in creating an object. Any
object instance of any class has a __proto__ property and it
uses __proto__ to access to that class’s prototype object as
__proto__ is a reference to it. It’s the __proto__ that lets
any object know where to look when it doesn’t have a method
or property of its own; when an object needs to use a shared
method or property within that objects inheritance chain.
The __proto__ property makes up the links in that chain.
[ __proto__
links classes with subclasses (prototypes) ]
What happens when you reference any property or any
method in an object is that Flash goes through that object
and looks through all the properties contained directly
within that object. These include any properties put there
by that object’s constructor or objects that you
specifically defined in the instance manually. If the
referenced property isn’t found there, Flash then looks to
the next object in the inheritance chain – the object the
current object is sharing properties from, most likely, the
prototype of that object’s class. Flash knows how to get to
that object based on the __proto__ property as it’s the
reference defining that connection. If the property still
isnt found in that object, Flash continues to search through
the __proto__ reference in that object to go up the chain
further until there is no more objects to search (or until
the property trying to be accessed is found).
The only object that doesn’t have a __proto__ property is
Object.prototype as it is the end of the line for object
inheritcance. Since all objects by default inherit from the
Object class the object class itself cannot inherit from
anything else. Otherwise there would be an infinite loop in
inheritance since every __proto__ would eventually come back
around to Object.prototype. For example, simply put the
following lines in a Flash movie and test it.
- Object.prototype.__proto__ = {};
In return you’ll get a repeating loop of trace message
errors saying:
"256 levels of prototype chain were exceeded. This is
probably a circular prototype chain. Further execution of
actions has been disabled in this movie."
The reason being that if Object.prototype’s __proto__ was
to another object, since that object’s __proto__ references
back to Object.prototype, you have yourself a circular,
non-ending prototype as the error indicated.