本文介绍了在 Backbone 中访问父类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从继承的 MyModel
类内部调用父类的 initialize
方法,而不是像我今天所做的那样完全覆盖它.>
我怎么能这样做?
这是我的代码现在的样子:
BaseModel = Backbone.Model.extend({初始化:函数(属性,选项){//做父级的东西}});MyModel = BaseModel.extend({初始化:函数(){//调用 BaseModel.initialize();//继续为这个子类做特定的事情.},});
解决方案
MyModel = BaseModel.extend({初始化:函数(){MyModel.__super__.initialize.apply(this, arguments);//继续为这个子类做特定的事情.},});
I need to call the initialize
method of the parent class, from inside the inherited MyModel
-class, instead of completely overwriting it as I am doing today.
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.
},
});
解决方案
MyModel = BaseModel.extend({
initialize: function() {
MyModel.__super__.initialize.apply(this, arguments);
// Continue doing specific stuff for this child-class.
},
});
这篇关于在 Backbone 中访问父类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!