A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/of below:

Array.of() - JavaScript | MDN

Array.of()

Baseline Widely available

Array.of() 方法會由引數(arguments)的數量來建立一個新的 Array 實體,而不管引數的數量或類型為何。

Array.of() 與 Array 建構式之間的不同在於如何處理整數引數:Array.of(7) 會建立一個擁有單個元素—7—的陣列,而 Array(7) 會建立一個 length 屬性值為 7 的空陣列(**註:**這意味著這個陣列有 7 個空缺欄位(empty slots),而非 7 個值為 undefined 的欄位)。

嘗試一下
console.log(Array.of("foo", 2, "bar", true));
// Expected output: Array ["foo", 2, "bar", true]

console.log(Array.of());
// Expected output: Array []
語法
Array.of(element0)
Array.of(element0, element1)
Array.of(element0, element1, /* … ,*/ elementN)
參數
elementN

要用來成為新建立之陣列的元素。

回傳值

一個新的 Array 實體。

描述

此函式是 ECMAScript 2015 標準的一部分。更多資訊可參考 Array.of and Array.from proposal 以及 Array.of polyfill。

範例 使用 Array.of()
Array.of(1); // [1]
Array.of(1, 2, 3); // [1, 2, 3]
Array.of(undefined); // [undefined]
在非陣列建構中使用 of()

可以在接受表示新陣列長度的單個參數的任何建構式上調用 of() 方法。

function NotArray(len) {
  console.log("NotArray called with length", len);
}

console.log(Array.of.call(NotArray, 1, 2, 3));
// NotArray called with length 3
// NotArray { '0': 1, '1': 2, '2': 3, length: 3 }

console.log(Array.of.call(Object)); // [Number: 0] { length: 0 }

When the this value is not a constructor, a plain Array object is returned.

console.log(Array.of.call({}, 1)); // [ 1 ]
Polyfill

若所有執行環境沒有原生支援,可以在其他程式之前先執行以下程式碼來建立 Array.of()。

if (!Array.of) {
  Array.of = function () {
    return Array.prototype.slice.call(arguments);
  };
}
規範 瀏覽器相容性 參見

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4