forEach = function( ary, fn ){
for ( var i = 0, l = ary.length; i < l; i++ ){
var c = ary[ i ];
if ( fn.call( c, i, c ) === false ){
return false;
}
}
}
forEach([1,2,3],function(i,el){
console.log(i,el);
});
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
用call是为了保证this指向的是迭代对象自身。
参考链接
上个代码进一步说明一下:
然后是没有call的版本:
可以打开chrome的console验证一下,(⊙o⊙)…
为了this