Hok's Macromedia Flash Blog

macromedia flash actionscript scripting, php, remoting, webservices, c#, javascript

Ads

Search

Inheritance: addClass revised

After i have found the failure of our addClass-method,
i took a deep watch to the proto-chain, constructor,
__constructor__ things.

Well, in the first moment my picture was off a big
anaconda-snake which i had to wrestle to become
sir of the situation, but a few traces later i got back
the controll over the situation.




Hence, i have the method-complete. And i think it
realy cool tool for inheritance.

Till now i have found the following Advantages and
Disadvantages:

Disadvantages


  • Not Macromedia recommended inheritance-way, but
    it does nothing more than using the proto-way of inheritance.

  • It is possible that other programmers do not see that
    an object have inherited, so more conventions and/or comments are
    needed.


Advantages

  • Object.registerClass allowes that you associate only one MC with
    one Element in the Libary. With the addClass-method it is possible to
    attach the clip and than set the class.

  • It is easy possible to attach a class to an existing mc.

  • It is possible to add unlimented classes to an object.

  • You can dynamicly add classes to your Objects. So it very
    comfortable to construct factorys, which have in their hand to
    controll the specialisation of the generated objects.


Please tell me what you think, did you see more pros and cons?
if (Object.prototype.addClass != undefined) {
   trace('ERROR: Method with the name Object.prototype.addClass is allready defined!');
}
Object.prototype.addClass = function (pConstructor) {
   //
   // Adds a class to an existing as-obj or mc-obj.
   //
   // The added class will be interjected to the
   // beginning of the objects proto-chain.
   // The super-operator works than in the new
   // chain-order.
   // The class to add can be specialized(inherited).
   //
   // Tests i have done:
   // - Multiple inheterence
   // - multiple call of addClass
   // - using with mcs
   // - using with mcs which was Object.registerClassed
   // - Allways i taked a deep watch to the generated
   //  proto-chain and allways compared to the proto-chain
   //  that comes out by standard inheritance and i have
   //  found no difference. So polimorphy works still fine.
   //
   // Untested
   // - it should also no problem to add an Class
   //  to a prototype.__proto__ for the multiple
   //  extending of a Class
   //
   var tmp = this.__proto__;
   this.__proto__ = pConstructor.prototype;
   var pp, p = this.__proto__;
   while (pp = p.__proto__) {
      if (pp == Object.prototype || pp == MovieClip.prototype) {
        p.__proto__ = tmp;
        p.constructor = this.constructor;
        p.__constructor__ = (tmp == MovieClip.prototype) ? MovieClip : this.__constructor__;
        this.constructor = pConstructor;
        this.__constructor__ = pConstructor;
        return true;
      } else {
        p = pp;
      }
   }
   return false;
}

Posted by hOk at April 30, 2003 04:57 PM

Comments (6)

So i have just talked colleague, and seems that
the method in rarly cases make sense. Normally
it will not needed.
I think if you have the case of an factory
that specializes objects then it would be usefull.
Another case could be if you want to change
the class of an object, but therefore we need
another allready not existing method removeClass.
regards, Holger

Posted by hOk at April 30, 2003 06:20 PM

Holger,

this is exactly what i was looking for, Thanks! Don't let your colleague get you down, there is plenty of use for this method.
regards, ButtSparkler

Posted by ButtSparkler at May 29, 2003 09:09 PM

Thank you so much!
Holger, do you mind if i use this method for something i plan to distribute freely?

Posted by deadlydentures at June 27, 2003 07:27 AM

Yes, all things in this block
can be used freely for everything you want.
regards, Holger

Posted by hOk at June 27, 2003 07:38 AM

Cool! I'm going to use it too in that case :)

hOk, you should start add a chat room or something here too, I think it would be nice to discuss with other visitors in real time.

Posted by online coupons at November 1, 2003 10:34 PM

I can't succeed to make it work... Can you give an example of use for multiple inheritance plese... Thanks...

Posted by Banane masquée at April 30, 2004 09:56 AM


Name:


Email Address:


URL:


Comments:



You can use <code>code that should be highlightned</code> to highlight code!
Optionally you can use the attribut language="php|perl|java" within the code tag, otherwise
actionscript-highlightning will be used.

New Place