国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

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

2019-11-20 12:20:04
字體:
來源:轉載
供稿:網友

 JavaScript數(shù)組的 forEach()方法調用數(shù)組中的每個元素。
語法

array.forEach(callback[, thisObject]);

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

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

返回值:

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

這種方法是一個JavaScript擴展到ECMA-262標準;因此它可能不存在在標準的其他實現(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>

這將產生以下結果:

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 资兴市| 南郑县| 蕲春县| 卢龙县| 舒兰市| 定安县| 鸡泽县| 吉林市| 林甸县| 深水埗区| 通城县| 台东县| 平湖市| 内黄县| 和林格尔县| 淄博市| 高陵县| 珠海市| 特克斯县| 资源县| 建平县| 永宁县| 井冈山市| 东山县| 新疆| 南郑县| 广饶县| 祁东县| 富裕县| 九寨沟县| 开远市| 旬邑县| 旺苍县| 共和县| 运城市| 古蔺县| 无极县| 河南省| 大港区| 鞍山市| 诸城市|