javascript - 是否需要缓存 'this' 并在 backbone.js 中设置其上下文?

标签 javascript backbone.js this

此代码来自 backbone.js,在 View 的渲染方法中。我注意到作者将“this”缓存到 var“that”,并将上下文设置为 this。是否有任何理由既缓存“this”又显式设置上下文?它似乎只与其中一个一起工作。

render: function () {
    var that = this;
    _.each(this.collection.models, function (item) {
        that.renderContact(item);
    }, this);
},

最佳答案

这应该很简单

this.collection.each(this.renderContact, this);

将完成上面代码试图做的任何结果

关于javascript - 是否需要缓存 'this' 并在 backbone.js 中设置其上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24180408/

相关文章:

javascript - 如何让 arbor.js 在 Internet Explorer 上运行?

javascript - 无法覆盖对象/类上的 toString

reactjs - 在 React 中对类属性使用箭头函数。不清楚

java - Java中有 "this"的名称吗?

javascript - 如何根据集合属性设置Backbone模型的默认值

javascript - 这 vs $(这)

javascript - 使用来自 ajax 下拉菜单的 onclick 事件更新隐藏的输入值

javascript - 获取所有连接的peer(Peer.js)

javascript - 范围超过 Canvas 的奇怪错误

javascript - Backbone.js View 的适当粒度是多少?