在开发我最新的 Web 应用程序并需要使用该Array.forEach
功能时,我不断发现以下代码用于添加对没有内置该功能的旧浏览器的支持。
/**
* Copyright (c) Mozilla Foundation http://www.mozilla.org/
* This code is available under the terms of the MIT License
*/
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fun /*, thisp*/) {
var len = this.length >>> 0;
if (typeof fun != "function") {
throw new TypeError();
}
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in this) {
fun.call(thisp, this[i], i, this);
}
}
};
}
我完全理解代码的作用以及它是如何工作的,但我总是看到它被复制,形式thisp
参数被注释掉,而是被设置为局部变量arguments[1]
。
我想知道是否有人知道为什么要进行此更改,因为据我所知,代码thisp
作为形式参数而不是变量可以正常工作?