if (!Array.prototype.forEach)
{
Array.prototype.forEach = function(fun /*, thisp*/)
{
var len = this.length;
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);
}
};
}
爲例:
<html>
<head>
<title>JavaScript Array forEach Method</title>
</head>
<body>
<script type="text/javascript">
if (!Array.prototype.forEach)
{
Array.prototype.forEach = function(fun /*, thisp*/)
{
var len = this.length;
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);
}
};
}
function printBr(element, index, array) {
document.write("<br />[" + index + "] is " + element);
}
[12, 5, 8, 130, 44].forEach(printBr);
</script>
</body>
</html>
來源:http://www.tutorialspoint.com/javascript/array_foreach.htm
你試過['Array.forEach'](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach)? – Zeta 2012-03-28 10:18:04
什麼試圖完全實現? 'magicRun'應該做什麼?你期望這個'paramsArray'應該發生什麼? – jAndy 2012-03-28 10:23:41