【发布时间】:2011-05-11 18:49:40
【问题描述】:
在 MDC 中有大量代码 sn-ps 用于在不支持它们的浏览器中实现对新 ECMAScript 标准的支持,例如 Array.prototype.map 函数:
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
res[i] = fun.call(thisp, t[i], i, t);
}
return res;
};
}
使用这个函数有什么好处(如果有的话)
function(fun, thisp)
{
// same code, just without the "var thisp = arguments[1];" line:
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = new Array(len);
for (var i = 0; i < len; i++)
{
if (i in t)
res[i] = fun.call(thisp, t[i], i, t);
}
return res;
}
,var t = Object(this); 而不是 var t = this; 和 var len = t.length >>> 0; 而不是 var len = t.length;?
【问题讨论】:
标签: javascript