国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 拉孜县| 巍山| 曲靖市| 江都市| 城步| 邢台县| 建湖县| 竹山县| 嘉义县| 郎溪县| 德州市| 长子县| 隆尧县| 华池县| 长治县| 双辽市| 屯留县| 广州市| 绥滨县| 平凉市| 黔东| 宜阳县| 辛集市| 禹州市| 安仁县| 太原市| 察雅县| 凉城县| 金湖县| 长治县| 泾源县| 保靖县| 乡宁县| 内江市| 白玉县| 界首市| 天全县| 满城县| 黑龙江省| 扬州市| 舞钢市|