1

我是 JS 新手并试图覆盖 Craftyjs 中的一个函数,这是我的代码:

    Crafty.c('TestA', {
        init: function() {                
            Crafty.addEvent(this, Crafty.stage.elem, "mousedown", this._e_clickListener);
        },
        _e_clickListener: function(e){
            this.foo();
        },
        foo: function() {
            console.log('call A foo');
        }
    });

    Crafty.c('TestB', {
        init: function() {
            this.requires('TestA');                
        },
        foo: function(){
            console.log('call B foo');
        }
    });

    Crafty.e('TestB');

当我点击画布时的结果是:

    call A foo

有没有办法把它变成'call B foo'呢?谢谢。

4

1 回答 1

3

我对CraftyJS不是很熟悉,文档可以使用一点TLC - 除非我错过了一些看起来好像调用requires只是将所有内容合并到一个对象中的东西 -后进优先。没有继承所以没有超类——只有一个平面对象。

也许有更好的方法,但我只能通过在导入所需组件简单地在函数内部分配它来“覆盖”函数。init

Crafty.c('TestB', {
    init: function() {
        this.requires('TestA');
        this.foo = function(){
            console.log('call B foo');
        }
    }
});

jsFiddle

于 2014-01-03T16:06:47.460 回答