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
Javascript 子视图事件回调是否优先?_Javascript_Backbone.js_Ecmascript 6 - Fatal编程技术网

Javascript 子视图事件回调是否优先?

Javascript 子视图事件回调是否优先?,javascript,backbone.js,ecmascript-6,Javascript,Backbone.js,Ecmascript 6,我有一些相关的观点: 第一: App.Views.TreeGrowthBase = App.Views.TreeGrowthBase.extend({ events: { 'submit form': 'submitForm', ... 然后在同一个文件中: submitForm: function(e) { e.preventDefault(); 以及应用程序中的其他位置: App.Views.WineTreeGrowthBase = App.Views.TreeGro

我有一些相关的观点:

第一:

App.Views.TreeGrowthBase = App.Views.TreeGrowthBase.extend({
  events: {
    'submit form': 'submitForm',
...
然后在同一个文件中:

submitForm: function(e) {
    e.preventDefault();
以及应用程序中的其他位置:

App.Views.WineTreeGrowthBase = App.Views.TreeGrowthBase.extend({
  submitForm(event) {
    event.preventDefault();
我的问题:在最后一段代码中。。。语法是什么:

submitForm(event) {
    event.preventDefault();
这是一个方法调用吗?定义一种方法?冒号在哪里

哪个优先?我想象子视图的
submitForm
方法定义发生了。。。如果它是一个方法定义

这是ES6(ECMAScript 2015)中新增的方法定义速记

相当于

submitForm: function submitForm(event) {
    event.preventDefault();
速记语法使用命名函数而不是匿名函数 函数(如
foo:function(){}
)。可以调用命名函数 从函数体(这对于匿名函数是不可能的 没有可参考的标识符)。有关更多详细信息,请参阅

并在具有可用新功能的浏览器中工作(如IE以外的浏览器)

覆盖函数 主干类的子类中重写的任何方法(扩展函数的结果)优先于父函数。如果要调用父函数,仍然可以:

submitForm: function(event) {
    // Using the Backbone '__super__'
    ThisClass.__super__.submitForm.apply(this, arguments);
    // Or the JavaScript preferred way
    ParentClass.prototype.submitForm.apply(this, arguments);
    event.preventDefault();
}
这不是主干网特有的。这是原型链的正常行为。主干只是将复杂性包装在一个简单的框架中

有关更多信息,请参见此



不要使用
this.constructor.\uuuuu super\uuuu
,因为没有保证它是实际的类,并且它可能是子类的构造函数,从而导致调用堆栈溢出。喜欢MyCurrentClass.\uuuuuuuuuuuuuuuuuuuuuuu,它是显式的,并且关闭了潜在扩展问题的大门。

它必须是
this.constructor.\uu super\uuuu
,它不能是
this.\uuuu super\uuuuuuuuuu
?@Jwan622这是因为
this.constructor==ChildClass
this.constructor.\uu super\uuuuuu==ChildClass.\uu super\uuu==ChildClass.\uuParentClass.prototype
。我喜欢
this.construtor.\uuuuu super\uuuu
,因为它从函数中删除了父函数的硬编码名称,并且在更改子函数扩展的父函数时可能避免难以跟踪的错误。在查看主干的源代码时可以更清楚地看到。@Jwan622我更新了我的答案,以通知您和读者有关
此.constructor.\uuuu super\uuu
的潜在问题。
submitForm: function(event) {
    // Using the Backbone '__super__'
    ThisClass.__super__.submitForm.apply(this, arguments);
    // Or the JavaScript preferred way
    ParentClass.prototype.submitForm.apply(this, arguments);
    event.preventDefault();
}