最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解JavaScript中的forEach()方法的使用

 更新時(shí)間:2015年06月08日 12:08:42   投稿:goldensun  
這篇文章主要介紹了詳解JavaScript中的forEach()方法的使用,是JS入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

 JavaScript數(shù)組的 forEach()方法調(diào)用數(shù)組中的每個(gè)元素。
語(yǔ)法

array.forEach(callback[, thisObject]);

下面是參數(shù)的詳細(xì)信息:

  •     callback : 函數(shù)測(cè)試數(shù)組的每個(gè)元素。
  •     thisObject : 對(duì)象作為該執(zhí)行回調(diào)時(shí)使用。

返回值:

返回創(chuàng)建數(shù)組。
兼容性:

這種方法是一個(gè)JavaScript擴(kuò)展到ECMA-262標(biāo)準(zhǔn);因此它可能不存在在標(biāo)準(zhǔn)的其他實(shí)現(xiàn)。為了使它工作,你需要添加下面的腳本代碼的頂部:

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>

這將產(chǎn)生以下結(jié)果:

[0] is 12
[1] is 5
[2] is 8
[3] is 130
[4] is 44 

相關(guān)文章

最新評(píng)論

夏河县| 平和县| 行唐县| 营山县| 青岛市| 游戏| 商洛市| 无棣县| 陇南市| 博罗县| 渭源县| 横峰县| 桂平市| 洛隆县| 伊金霍洛旗| 安西县| 江阴市| 将乐县| 遂溪县| 十堰市| 两当县| 城固县| 阿克陶县| 郯城县| 休宁县| 壶关县| 安新县| 达尔| 高阳县| 湾仔区| 蛟河市| 通道| 曲阳县| 延川县| 南华县| 博乐市| 都江堰市| 南丹县| 石台县| 宁德市| 唐海县|