JavaScript 陣列map()方法建立一個新的陣列使用呼叫此陣列中的每個元素上所提供的.函式的結果。
語法
(callback[, thisObject]);
下面是引數的詳細資訊:
callback : 從當前的元素函式產生新的陣列的元素。 thisObject : 物件作為該執行回撥時使用
返回值:
返回建立陣列
相容性:
這種方法是一個JavaScript擴充套件到ECMA-262標準;因此它可能不存在在標準的其他實現。為了使它工作,你需要新增下面的指令碼程式碼在頂部:
if (!){ = function(fun /*, thisp*/) { var len = th; 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 this) res[i] = (thisp, this[i], i, this); } return res; };}
例子:
if (!){ = function(fun /*, thisp*/) { var len = th; 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 this) res[i] = (thisp, this[i], i, this); } return res; };}var numbers = [1, 4, 9];var roots = ();e("roots is : " + roots );
這將產生以下結果:
roots is : 1,2,3