I am upgrading from 1.5.1 to 1.6 and, while trying to use the
inheritance system from Prototype, I'm a little puzzled on how the
implementation was designed.

Let say I have this object :

var baseObj = Class.create({
  initialize: function() {
    this.foo = function() { alert( "Hello world!" ); };
  }
});

And this child object extending baseObj :

var childObj1 = Class.create(baseObj, {
  initialize: function() {
    this.bar = function() { alert( "bleh" ); };
  }
});


In this implementation, the method childObj1.foo(); is not a
function... as it is a "private" member in the base class.

If I do this :

var childObj2 = Class.create();
Class.inherit(childObj2, baseObj);    // even if I add a third
parameter called "base"

Then Prototype causes an error when executing "new childObj2();"


Now, this is the piece of code that I was using prior to Prototype
1.6 :

/**
 * Inheritance support
 *
 * Source : 
http://www.someelement.com/2007/03/multiple-inheritance-with-prototypejs.html
 *
 * Usage (simple) :
 *
 *    var Class1 = Class.create();
 *     ....
 *    var Class2 = Class.create();
 *    Object.inherit(Class2, Class1);
 *    Object.extend(Class2, {
 *      initialize: function() {
 *        this.base();
 *        ....
 *      }
 *    } );
 *
 * Usage (multiple) :
 *
 *    var Class3 = Class.create();
 *    Object.inherit(Class3, Class1, "baseClass1"); // baseClass1 =
constructeur Class1
 *    Object.inherit(Class3, Class2, "baseClass2"); // baseClass2 =
constructeur Class2
 *    Object.extend(Class3, {
 *      initialize: function() {
 *        this.baseClass1();
 *        this.baseClass2();
 *        ...
 *      }
 *    } );
 *
 */
Object.inherit = function(subClass, baseClass, baseFuncName) {
  function inheritance() {}
  inheritance.prototype = baseClass.prototype;
  Object.extend(subClass.prototype, new inheritance());
  subClass.prototype.constructor = subClass;
  if (!baseFuncName || baseFuncName == "")
      baseFuncName = "base";
  subClass.prototype[baseFuncName] = baseClass.prototype.initialize;
};

Which works great and can support multi inheritances.

What are your thoughts on this ? Thanks.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to