开发者

AS3 Prototypes - are they just static variables?

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

A class’s prototype object is a special instance of that class that provides a mechanism for sharing state across all instances of a class. At run time, when a property is not found on a class instance, the delegate, which is the class prototype object, is checked for that property. If the prototype object does not contain the property, the process continues with the prototy开发者_开发技巧pe object’s delegate checking in consecutively higher levels in the hierarchy until Flash Player or the Adobe Integrated Runtime finds the property.

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

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


Not exactly, a function implemented as a prototype is still executed as instance method. In a static function you don't have access to this.

Also it doesn't mean setting a prototype value to something is setting the value for every instance. It's only the fallback value, if an object of that class isn't setting it explicitly.

var o1:Object= {};
var o2:Object= {};
Object.prototype.foo = "foo";

o1.foo = "bar"

trace(o1.foo) // bar
trace(o2.foo) // foo
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜