gpt4 book ai didi

javascript - Backbone View 中的每个方法后面都需要 "return this"吗?

转载 作者:行者123 更新时间:2023-11-28 11:10:26 25 4
gpt4 key购买 nike

在 Backbone View 中的每个方法的末尾是否需要“return this”?

render: function() {
this.editElem();
// I realize it wouldn't be necessary here, but...
},

renderElem: function() {
this.$el.addClass('foobar');
return this // is this one necessary?
},

编辑这个例子怎么样?

render: function() {
this.editElem();
},

renderElem: function() {
this.$el.addClass('foobar');
return this;
},

最佳答案

我没有使用过 Backbone,所以我不确定它是否是必需的(可能不是),但您通常会看到使用该模式,以便您可以将函数调用“链接”在一起。它被称为 fluent interface .

例如,使用这种模式,您可以编写紧凑的代码,例如:

myVariable.editElem().somethingElse().anotherMethod();

关于javascript - Backbone View 中的每个方法后面都需要 "return this"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11941892/

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