【发布时间】:2018-05-24 05:40:22
【问题描述】:
请浏览以下代码。 我无法确切知道 calllback.call 正在做什么。
另外,我不知道 this 和 this[i] 之间有什么区别,以及 if(callback.call(this, this[i])) 如何评估为真或假。
Array.prototype.each = function(callback) {
var i = 0;
while (i < this.length) {
callback.call(this, this[i]);
i++;
}
return this;
};
Array.prototype.map = function(callback) {
var i = this.length;
var found = []
while (i--) {
if(callback.call(this, this[i])) {
found.push(this[i]);
}
}
return found;
};
函数调用如下:
Array.each(function(value){
...
})
Array.map(function(value){
...
})
【问题讨论】:
-
在这两种情况下,
each和map都被分配了一个函数。 -
你不会调用像
[1,2,3].each(function(){ /* do something */})这样的函数吗?
标签: javascript jquery arrays callback call