Javascript 在 Backbone 中访问父类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8970606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:15:32  来源:igfitidea点击:

Accessing parent class in Backbone

javascriptoopclassinheritancebackbone.js

提问by Industrial

I need to call the initializemethod of the parent class, from inside the inherited MyModel-class, instead of completely overwriting it as I am doing today.

我需要initialize从继承的MyModel类内部调用父类的方法,而不是像我今天所做的那样完全覆盖它。

How could I do this?

我怎么能这样做?

Here's what my code looks right now:

这是我的代码现在的样子:

BaseModel = Backbone.Model.extend({
    initialize: function(attributes, options) {
        // Do parent stuff stuff
    }
});

MyModel = BaseModel.extend({
    initialize: function() {
        // Invoke BaseModel.initialize();
        // Continue doing specific stuff for this child-class.
    },
});

采纳答案by Raynos

MyModel = BaseModel.extend({
    initialize: function() {
        MyModel.__super__.initialize.apply(this, arguments);
        // Continue doing specific stuff for this child-class.
    },
});

回答by Yury Tarabanko

Try

尝试

MyModel = BaseModel.extend({
    initialize: function() {
        BaseModel.prototype.initialize.apply(this, arguments);
        // Continue doing specific stuff for this child-class.
    },
});

回答by rookieRailer

This worked for me, when I was trying to inherit among my models:

这对我有用,当我试图在我的模型中继承时:

MyModel.prototype.initialize.call(this, options);

Referenced from http://documentcloud.github.com/backbone/#Model-extend

引用自http://documentcloud.github.com/backbone/#Model-extend

Thanks.

谢谢。

回答by wheresrhys

I think it'd be

我想会是

MyModel = BaseModel.extend({
    initialize: function() {
        this.constructor.__super__.initialize.call(this);
        // Continue doing specific stuff for this child-class.
    },
});

回答by ErichBSchulz

this seems to be almost a duplicate of Super in Backbone, so you want something like this:

这似乎几乎是BackboneSuper的副本,所以你想要这样的东西:

Backbone.Model.prototype.initialize.call(this);

回答by culurienneldoreth

Similar to @wheresrhys, but I would use apply instead of call in case BaseModel.initialize is expecting arguments. I try to avoid processing the attributes map that can be passed to a Backbone Model upon initialization, but if the BaseModel were actually a View or a Collection then I might want to set options.

与@wheresrhys 类似,但我会使用 apply 而不是 call 以防 BaseModel.initialize 需要参数。我尽量避免在初始化时处理可以传递给主干模型的属性映射,但如果 BaseModel 实际上是一个视图或集合,那么我可能想要设置选项。

var MyModel = BaseModel.extend({
    initialize: function() {
        this.constructor.__super__.initialize.apply(this, arguments);
        // Continue doing specific stuff for this child-class.
    },
});

回答by Bnaya

here's a multi generation callSuper method, just add it to your extending class.

这是一个多代 callSuper 方法,只需将其添加到您的扩展类中即可。

callSuper: function (methodName) {
    var previousSuperPrototype, fn, ret;

    if (this.currentSuperPrototype) {
        previousSuperPrototype = this.currentSuperPrototype;
        // Up we go
        this.currentSuperPrototype = this.currentSuperPrototype.constructor.__super__;
    } else {
        // First level, just to to the parent
        this.currentSuperPrototype = this.constructor.__super__;
        previousSuperPrototype = null;
    }

    fn = this.currentSuperPrototype[methodName];

    ret = (arguments.length > 1) ? fn.apply(this, Array.prototype.slice.call(arguments, 1)) : fn.call(this);

    this.currentSuperPrototype = previousSuperPrototype;

    return ret;
}

回答by Dani Cricco

You might consider rewriting your code using functional inheritance.

您可能会考虑使用函数继承来重写代码。

var BackBone=function(){
    var that={};

    that.m1=function(){

   };
   return that;

};

var MyModel=function(){

 var that=BackBone();
 var original_m1=that.m1;

//overriding of m1
 that.m1=function(){
    //call original m1
 original_m1();
 //custom code for m1
  };
};