我有可变数量的数组,我想提交给 underscore.js _.difference
.如何将它们提交给此功能?
var arrays = [[1,2,3], [3,4,5], [6,3,6]];
var result = _.difference.apply(null, arrays);
似乎有效。但我不确定这是否是 apply()
的用途。有没有更好的方法?
最佳答案
您的解决方案看起来不错,但我会将上下文更改为 Underscore 对象本身:
var result = _.difference.apply(_, arrays);
它的工作原理是一样的,因为 this
keyword is not used inside the method , 但最好保留上下文。
E. G。当你在自己的 mixin 中使用 this
时,不保留上下文会破坏它:
_.mixin({
getVersion: function() {
return this.VERSION;
}
});
_.getVersion(); // '1.4.4'
_.getVersion.apply(null, []); // undefined
_.getVersion.apply(_, []); // '1.4.4'
关于javascript - 提交可变数量的数组以强调 _.difference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19842808/