gpt4 book ai didi

javascript - subview 事件回调是否优先?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:42:09 24 4
gpt4 key购买 nike

我有几个相关的 Backbone 观点:

首先:

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();

那是方法调用吗?定义方法?冒号在哪里?

哪个优先?我想象 subview 的 submitForm 方法定义发生了......如果它是一个方法定义?

最佳答案

Method definition shorthand

submitForm(event) {
event.preventDefault();

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

相当于

submitForm: function submitForm(event) {
event.preventDefault();

The shorthand syntax uses named function instead of anonymous functions (as in foo: function() {}). Named functions can be called from the function body (this is impossible for anonymous function as there is no identifier to refer to). For more details, see function.

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

覆盖函数

在 Backbone 类的子类(extend 函数的结果)中覆盖的任何方法都优先于父函数。如果您想调用父函数,仍然可以:

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();
}

这不是 Backbone 特有的。这是原型(prototype)链的正常行为。 Backbone 只是将复杂性包装在一个简单的 extend function 中.

查看此 in-depth answer了解更多信息。


不要使用 this.constructor.__super__因为它不能保证是实际的类,它可能是子类的构造函数,导致调用堆栈溢出。赞成MyCurrentClass.__super__ ,它是明确的,并为潜在的扩展问题关闭了大门。

关于javascript - subview 事件回调是否优先?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41230919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com