【发布时间】:2011-12-02 05:59:28
【问题描述】:
在开发我最新的 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 作为形式参数而不是变量,该代码可以正常工作?
【问题讨论】: