Mootools类扩展了调用重写的父方法

时间:2012-06-12 21:40:58

标签: javascript inheritance mootools

我正在玩Mootools类继承等,我试图用.parent属性调用基类重写方法(长代码片段的sry)。

var app = this.app = {},
    //ultimate base class
    Animal = new Class({
        initialize : function(param){
            this.age = param.age;
            this.name = param.name;
        },
        doStuff : function(param){
            alert("animal doStuff");
        }
    }),
    //herbivore functionality
    HerbivoreBehaviour = new Class({
        eat : function(){
            alert('Plants are scrumptious');
        }
    }),
    //carnivore functionality
    CarnivoreBehaviour = new Class({
        eat : function(){
            alert('Meat tastes gooood');
        }
    })
    //mammal base class
    Mammal = new Class({
        Extends : Animal,
        initialize : function(param){
            this.parent(param);
            this.numberOfNipples = param.numberOfNipples;
        },
        doStuff : function(){
            alert("mammal doStuff");
            this.doStuff.parent();
        }
    }),
    //reptile base class
    Reptile = new Class({
        Extends : Animal,
        initialize : function(param){
            this.parent(param);
            this.numberOfScales = param.numberOfScales;
        },
        doStuff : function(){
            alert('reptile doStuff');
            this.doStuff.parent();
        }
    }),
    //final top class animal->mammal-cow!
    //and it's a herbivore
    Cow = new Class({
        Extends : Mammal,
        Implements : HerbivoreBehaviour,
        initialize : function(param){
            this.parent(param);
            this.isMooCrazy = param.isMooCrazy;
        },
        doStuff : function(){
            alert('I am a cow');
            this.doStuff.parent();
        }
    }),
    //final top level class animal->reptile->mutantLizard!
    //and it's a carnivore
    MutantLizard = new Class({
        Extends : Reptile,
        Implements : CarnivoreBehaviour,
        initialize : function(param){
            this.parent(param);
            this.isDestroyingEverything = param.isDestroyingEverything;
        },
        doStuff : function(){
            alert('STOMP STOMP STOMP CRRRRRASH');
            this.doStuff.parent();
        }
    });

    app.run = function(){
        var daisy = new Cow({
                name : 'Daisy',
                age : 2,
                numberOfNipples : 6,
                isMooCrazy : true
            }),
            godzilla = new MutantLizard({
                name : 'Godzilla',
                age : 1123,
                numberOfScales : 123456,
                isDestroyingEverthing : true
            });

        daisy.eat();
        godzilla.eat();
        daisy.doStuff();
        godzilla.doStuff();
    };

来自daisy.eat()和godzilla.eat()的当前输出是正确的,但是在daisy.doStuff()的第一个print语句之后执行停止然后它不会调用daisies parent class doStuff()方法我希望的是什么。你能告诉我怎么称呼doStuff吗?

更新:

我设法修复了他通过用this.parent()替换this.doStuff.parent()来调用序列。然而,这引发了一个问题,即“这个”正在受到什么影响。我认为这将是该类的实际实例,即。 daisy或godzilla,但在doStuff方法中使用this.parent()似乎意味着,至少对我而言,'this'指的是它当前所在的实际Function对象,除非调用parent()方法函数内部会自动查找您所使用的方法并调用它的第一个重写方法。

1 个答案:

答案 0 :(得分:2)

右。基本上,一切都很好,这是个好消息。唯一需要做的更改是将this.doStuff.parent()更改为this.parent()

http://jsfiddle.net/dimitar/njQBN/

似乎工作正常。

this.doStuff.parent()开始时并不是很干,它也引用了一个本地属性 - 它在一个类的上下文中,使用相同的方法扩展和实现其他类 - 同时具有。 ..意外的结果。

相关问题