On 3/21/07, Danny Kodicek <[EMAIL PROTECTED]> wrote:
Hmm - this seems to go against the stuff we've been talking about recently (see 'super and this' thread) where people have been saying that inherited classes are essentially wrapped into one. This is an example where the ancestor is being treated as an object in its own right. If inheritance worked the way people described in the previous thread, it ought not to be possible for a command to bypass the Child class and go straight to the Mother.
The ancestor /is/ an object in its own right. Every object has a property __proto__ linking it to its prototype object. For every class, there is one single prototype (which in turn has its __proto__ set to its superclass' prototype, and so on, up the Prototype Chain to Object.prototype), and every instance of that class, as well as all prototype objects of its child classes, has it's __proto__ set to it. Example: class Foo { var name = "foo"; function Foo () {} function hello () { trace( "hello " + name ); } } class Bar extends Foo { function Bar () { super(); name = "bar"; } } What this gives you is nothing but a bunch of objects of two types, some of type "object", some of type "function" (which extends Object itself), and a single string (the second string in Bar's constructor only gets created whenever you create another instance): Foo is a function, the constructor of a class, as is Bar. They're normal functions, but for one property, Foo.prototype (resp. Bar.prototype). That's one of the object that just got created, the class' prototype. That prototype object has all the properties and methods that instances of the class shall inherit as properties, in this case, a function called "hello", which therefore would be Foo.prototype.hello, and one property of type string called "name". If you had an instance of Foo, myFoo, that would really only be an object. It wouldn't have a property "hello" or "name" itself, in fact, it would only have two properties, one for the prototype and one for the constructor. When you instantiate it with 'new Foo()', here's what happens: // instantiate the object: var myFoo = { __proto__: Foo.prototype, constructor: Foo }; // and call the constructor on it: myFoo.constructor.apply( myFoo ); Every object has those two properties, by the way. Even when you say {} what you really get is {__proto__: Object.prototype, constructor: Object}. (So, you can "instantiate" an object without calling its constructor -- if you did this with Bar, the resulting myBar would trace "hello foo" when you did myBar.hello().) Foo.prototype looks like this: Foo.prototype = { __proto__: Object.prototype, constructor: Foo, name: "foo", hello: function () { trace( "hello " + this.name ); } }; ...and Bar's prototype: Bar.prototype = { __proto__: Foo.prototype, // that's the inheritance! constructor: Bar, }; You can access myFoo.name, but it doesn't have a method "hello" or a property "name" itself. When you try to access "name" as myFoo.name, the virtual machine checks if your instance has that property. It doesn't, so it goes on to check the class' prototype object, which is where "name" is found -- myFoo.__proto__.name == Foo.prototype.name. Bar.prototype has a __proto__ property itself, set to Foo.prototype, and would inherit "name" the same way. Instances of Bar, however, would overwrite it in the constructor. That gives each single instance of Bar a property "name" set to "bar", while there is only one property "name" set to "foo", shared by all instances of Foo (and, oddly, by Bar.prototype). All of the following are true: Bar.prototype.__proto__ == Foo.prototype myBar.__proto__ == Bar.prototype myBar.__proto__.__proto__ == Foo.prototype myBar.__proto__.__proto__.__proto__ == Object.prototype So what's super, really? I have no idea. 'typeof super' traces "object", but somehow you can call it like a function. In the constructor, it behaves just as this.__proto__.__proto__.constructor would, and in methods, it behaves just as if it was this.__proto__.__proto__. It's not equal (as in '==') to either, though. It's a mystery, at least to me, maybe somebody can explain? Mark _______________________________________________ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com