在Javascript主干类中调用超级方法
我有一个扩展主干.View的基类。我希望在重写子类上的“initialize”之后能够调用super的“initialize”。如何以最健壮、最清晰的方式在javascript中调用扩展类的super 我已经看到了这个(),有没有一种更清晰的方法来实现这一点,而不必事先知道谁是超级类在Javascript主干类中调用超级方法,javascript,backbone.js,Javascript,Backbone.js,我有一个扩展主干.View的基类。我希望在重写子类上的“initialize”之后能够调用super的“initialize”。如何以最健壮、最清晰的方式在javascript中调用扩展类的super 我已经看到了这个(),有没有一种更清晰的方法来实现这一点,而不必事先知道谁是超级类 App.Views.BaseView = Backbone.View.extend({ initialize: function(templateContext){ this.super_c
App.Views.BaseView = Backbone.View.extend({
initialize: function(templateContext){
this.super_called = true;
}
});
对于我的所有子视图,我希望利用已经编写的initialize函数
App.Views.ChildViewWorks = App.Views.extend({});
var newView = new App.Views.ChildViewWorks();
alert(newView.super_called); // print true
App.Views.ChildViewDoesNotWork = App.Views.extend({
initialize: function(templateContext){
this.super_called = false;
//what line of code can I add here to call the super initialize()?
}
});
var newViewWrong = new App.Views.ChildViewDoesNotWork();
alert(newViewWrong.super_called); //now equal to false because I have not called the super.
你能提供App.Views是如何定义的吗?最终的答案是,如果不明确知道super,就无法巧妙地调用super?显然,你可以将其存储在继承声明上下文中继承类的属性中。
App.Views.ChildViewDoesNotWork = App.Views.BaseView.extend({
initialize: function(templateContext){
this.super_called = false;
App.Views.BaseView.prototype.initialize.call(this);
}
});