在zepto.js中看到这样的代码:
if (Array.prototype.reduce === undefined)
Array.prototype.reduce = function(fun){
//...省略好多代码
accumulator = fun.call(undefined, accumulator, t[k], k, t);
//...省略好多代码
}
这里的参数'fun'调用使用了call的形式,有必要吗?有什么意义?
functionA.call(undefined) 和 functionA() 在我看来是没有什么区别的,他们内部的this都是指向windows对象.
补充测试:
<script>
var myMethod = {
reduce1: function(fn) {
fn.call(undefined);
},
reduce2: function(fn) {
fn();
}
}
var obj = {
sum: function() {
console.log('this in obj:', this);
}
}
var sum = function() {
console.log('this in global function:', this);
};
myMethod.reduce1(obj.sum);
myMethod.reduce2(obj.sum);
//this in obj: Window {external: Object, chrome: Object, document: document, myMethod: Object, arr: Array[5]…}
</script>
为了传参数为了传参数