我有多个要提交给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,它的工作原理相同,但是保留上下文是一个好习惯。例如当您在自己的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/