在Backbone视图中的每个方法之后都需要"返回此"吗?

Lei*_*mon 0 javascript backbone.js

在Backbone视图中的每个方法的末尾都需要"返回此"吗?

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?
},
Run Code Online (Sandbox Code Playgroud)

编辑 这个例子怎么样?

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

renderElem: function() {
this.$el.addClass('foobar');
return this;
},
Run Code Online (Sandbox Code Playgroud)

wsa*_*lle 8

我没有使用Backbone,所以我不确定它是否是必需的(可能不是),但你通常会看到使用的模式,这样你就可以将函数调用"链接"在一起.它被称为流畅的界面.

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

myVariable.editElem().somethingElse().anotherMethod();
Run Code Online (Sandbox Code Playgroud)