在处理我最新的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);
}
}
};
}
Run Code Online (Sandbox Code Playgroud)
我完全理解代码的作用以及它是如何工作的,但是我总是看到它被复制,并且正式thisp参数被注释掉并将其设置为使用的局部变量arguments[1]. …