1

我有一个扩展 Backbone.View 的基类。我希望能够在覆盖子类的“初始化”后调用超级的“初始化”。如何以最健壮和最清晰的方式完成在 javascript 中调用扩展类的超类?

我已经看到了这个(Super in Backbone),有没有更清晰的方法来完成这个,而不必事先知道谁是超级类?

App.Views.BaseView = Backbone.View.extend({
    initialize: function(templateContext){
        this.super_called = true;
    }
});

对于我所有的子视图,我想利用已经编写好的初始化函数。

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.
4

1 回答 1

2
App.Views.ChildViewDoesNotWork = App.Views.BaseView.extend({
    initialize: function(templateContext){
        this.super_called = false;
        App.Views.BaseView.prototype.initialize.call(this);
    }   
});
于 2013-08-12T17:50:04.507 回答