This project is archived and is in readonly mode.

#1089 ✓wontfix
Jan Jonas

Mootools 1.3 breaks Extends / Implements inheritance

Reported by Jan Jonas | November 15th, 2010 @ 12:43 PM

As described here http://blog.janjonas.net/2010-11-15/update-mootools-core-125-to-13-... after upgrading to MooTools 1.3 my inheritance structure is broken.

Example code:

var MyBaseClass = new Class({
    sayHello: function() {
        alert("BaseClass: Hello!");
    }
});
var MyInterface = new Class({
    sayHello: function() {
        alert("Interface: Hello!");
        this.parent();
    }
});
var MyClass = new Class({
    Extends: MyBaseClass
    ,Implements: MyInterface
});
var myClass = new MyClass();
myClass.sayHello();

Output/Result:
MooTools 1.2.5: Two alert boxes
MooTools 1.3: First alert box and error message "The method "sayHello" has no parent. [Break on this error] if (!previous) throw new Error('The method "' + name + '" has no parent.'); "

Comments and changes to this ticket

  • Christoph Pojer

    Christoph Pojer December 1st, 2010 @ 08:37 PM

    • State changed from “new” to “wontfix”

    This functionality is not supported by MooTools. If this worked in any previous version of MooTools it was indeed a bug.

    "Implements" does not affect the inheritance structure, ie. implementing any methods via Implements does not allow you to call "this.parent". This is what "Extends" is for.

    We do not have an implementation of multiple inheritance, which means that you can only extend one class but you can implement (mixin) many clases (see: http://en.wikipedia.org/wiki/Diamond_problem for more information).

    The class that you are trying to implement does not have a parent, only the class where you are using "Extends".

    To fix your structure you'd have to use "MyInterface extends MyBaseClass". Another solution would be to name the method "otherHello" in "MyInterface" and call "this.sayHello()" within "otherHello".

    Please note that what you posted above is not properly programmed. The Interface initially does not have a parent, yet you are trying to make use of a parent method.

  • Jan Jonas

    Jan Jonas December 2nd, 2010 @ 01:22 PM

    Hi Christopher,

    thank you for your detailed explanation. I understand the point that MyInterface does not extend an existing and therefore this.parent is undefined.

    I don't know the Mixin concepts in detail and I don't know how they are implemented in languages like Ruby or Perl, but as far as I understand the MooTools implement construct, it modifies an existing object by adding new functionality. And that is what I tried to do: In my example MyInterface should add a new "sayHello"-method to MyClass that needs access to the "sayHello"-method in the parent class of MyClass, which is MyBaseClass. I expected that the added functionality (sayHello in this example) is executed in the context of the object whose class implements the interface. And here this.parent should be defined.

    Maybe this example is not the best. The general situation is: A class B extends a class A and overrides a method m defined in A. The implementation of m in B uses the (super-)method m defined in the super-class A (by calling this.parent). Now you want to modify the functionality m in B by using an interface, e.g. because there is an other class C that extends B. In this modified functionality you also need to access the (super-)method m defined in A (because you don't want to re-implement the function). Is there a way to do this in MooTools?

    Unfortunately my project is to complex to use it as simple "real world" example.

    Of course, there are always workarounds to solve that problem and maybe I miss something. Altogether, the behavior in MooTools 1.2.5 seems to be more intuitive to me.

  • Christoph Pojer

    Christoph Pojer December 2nd, 2010 @ 01:41 PM

    "Unfortunately my project is to complex to use it as simple "real world" example."

    -- I'd argue that this means that your project is poorly designed, no offense. Web projects are usually not designed at all, even less the client side parts.

    In any case. The only functionality Implements provides (and should provide) is to mix in functions or properties from other classes. So you can add functionality, however you are not allowed to make assumptions about the inheritance structure. That's about it :)

  • Jan Jonas

    Jan Jonas December 12th, 2010 @ 12:47 PM

    I can't follow your argumentation that web projects are not designed. Of course in most cases the software design are much more complex on the server side, but IMHO there are good reasons to think about the software design on the client side.
    Anyway, thanks for your explanation. I will find an other solution for my OOP structures.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

People watching this ticket

Pages