在Javascript Backbone类中调用Super方法

Calling Super Method in Javascript Backbone Class

本文关键字:调用 Super 方法 Javascript Backbone      更新时间:2023-09-26

我有一个扩展Backbone.View的基类。我希望能够在重写子类上的"initialize"后调用super的"initiate"。如何以最健壮、最清晰的方式调用javascript中扩展类的super?

我已经看到了这一点(骨干中的超级),有没有一种更清晰的方法来实现这一点,而不必事先知道超级班是谁?

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.ChildViewDoesNotWork = App.Views.BaseView.extend({
    initialize: function(templateContext){
        this.super_called = false;
        App.Views.BaseView.prototype.initialize.call(this);
    }   
});