Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript主干类中调用超级方法_Javascript_Backbone.js - Fatal编程技术网

在Javascript主干类中调用超级方法

在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

我有一个扩展主干.View的基类。我希望在重写子类上的“initialize”之后能够调用super的“initialize”。如何以最健壮、最清晰的方式在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是如何定义的吗?最终的答案是,如果不明确知道super,就无法巧妙地调用super?显然,你可以将其存储在继承声明上下文中继承类的属性中。
App.Views.ChildViewDoesNotWork = App.Views.BaseView.extend({
    initialize: function(templateContext){
        this.super_called = false;
        App.Views.BaseView.prototype.initialize.call(this);
    }   
});