Are AS3 Prototypes Just Static Variables?

A reference to a prototype object of an object of a class or function. The prototype property is automatically created and attached to any class or function object that you create. This property is static because it is specific to the class or function you are creating. For example, if you create a class, the value of the prototype property is shared by all instances of the class and is only available as a property of the class. Instances of your class cannot directly access the prototype property.

The object of the class prototype is a special instance of this class, which provides a mechanism for sharing state in all instances of the class. At run time, when a property is not found in the class instance, the delegate that is the object of the class prototype is checked for this property. If the prototype object does not contain a property, the process continues with the delegate checking the prototype objects sequentially above the levels in the hierarchy until Flash Player or Adobe Integrated Runtime finds the property.

Note. In ActionScript 3.0, prototype inheritance is not the primary inheritance mechanism. Class inheritance, which controls the inheritance of fixed properties in class definitions, is the main inheritance in ActionScript 3.0.

So, from this I get the impression that prototypes are just static variables. I'm right?

+4
source share
1 answer

Not exactly, a function implemented as a prototype still executes as an instance method. In a static function, you do not have access to this .

In addition, this does not mean setting a prototype value for something setting a value for each instance. This is only a fallback value unless an object of this class sets it explicitly.

 var o1:Object= {}; var o2:Object= {}; Object.prototype.foo = "foo"; o1.foo = "bar" trace(o1.foo) // bar trace(o2.foo) // foo 
+7
source

All Articles