From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- .../global_objects/array/isarray/index.html | 134 +++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 files/zh-tw/web/javascript/reference/global_objects/array/isarray/index.html (limited to 'files/zh-tw/web/javascript/reference/global_objects/array/isarray/index.html') diff --git a/files/zh-tw/web/javascript/reference/global_objects/array/isarray/index.html b/files/zh-tw/web/javascript/reference/global_objects/array/isarray/index.html new file mode 100644 index 0000000000..f610cd1f54 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/array/isarray/index.html @@ -0,0 +1,134 @@ +--- +title: Array.isArray() +slug: Web/JavaScript/Reference/Global_Objects/Array/isArray +tags: + - Array + - ECMAScript5 + - JavaScript + - Method + - Reference + - polyfill + - 方法 + - 陣列 +translation_of: Web/JavaScript/Reference/Global_Objects/Array/isArray +--- +
{{JSRef}}
+ +

Array.isArray() 函式會檢查傳入的值是否為一個 {{jsxref("Array")}}。

+ +
Array.isArray([1, 2, 3]);  // true
+Array.isArray({foo: 123}); // false
+Array.isArray('foobar');   // false
+Array.isArray(undefined);  // false
+
+ +

語法

+ +
Array.isArray(obj)
+ +

參數

+ +
+
obj
+
要檢查的物件。
+
+ +

回傳值

+ +

若物件為 {{jsxref("Array")}} 回傳 true;否則回傳 false

+ +

描述

+ +

檢查傳入的物件是否為陣列({{jsxref("Array")}}),如果是便回傳 true,否則回傳 false

+ +

更多細節請參考 “Determining with absolute accuracy whether or not a JavaScript object is an array”

+ +

範例

+ +
// 下方都回傳 true
+Array.isArray([]);
+Array.isArray([1]);
+Array.isArray(new Array());
+Array.isArray(new Array('a', 'b', 'c', 'd'));
+Array.isArray(new Array(3));
+// 小細節:Array.prototype 本身是陣列:
+Array.isArray(Array.prototype);
+
+// 下方都回傳 false
+Array.isArray();
+Array.isArray({});
+Array.isArray(null);
+Array.isArray(undefined);
+Array.isArray(17);
+Array.isArray('Array');
+Array.isArray(true);
+Array.isArray(false);
+Array.isArray({ __proto__: Array.prototype });
+
+ +

instanceof vs isArray

+ +

當檢查 Array 實例時,Array.isArray 相較於 instanceof 更加推薦,因為它可以穿透 iframes

+ +
var iframe = document.createElement('iframe');
+document.body.appendChild(iframe);
+xArray = window.frames[window.frames.length-1].Array;
+var arr = new xArray(1,2,3); // [1,2,3]
+
+// 正確地檢查陣列型態
+Array.isArray(arr);  // true
+// 有害地,因為它不能在 iframes 之間正常運作
+arr instanceof Array; // false
+
+ +

Polyfill

+ +

如果 Array.isArray() 不存在於您的環境,在其他程式碼前執行下列程式碼可建置 Array.isArray()

+ +
if (!Array.isArray) {
+  Array.isArray = function(arg) {
+    return Object.prototype.toString.call(arg) === '[object Array]';
+  };
+}
+
+ +

規範

+ + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES5.1', '#sec-15.4.3.2', 'Array.isArray')}}{{Spec2('ES5.1')}}Initial definition. Implemented in JavaScript 1.8.5.
{{SpecName('ES6', '#sec-array.isarray', 'Array.isArray')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-array.isarray', 'Array.isArray')}}{{Spec2('ESDraft')}} 
+ +

瀏覽器相容性

+ +
+ + +

{{Compat("javascript.builtins.Array.isArray")}}

+
+ +

參見

+ + -- cgit v1.2.3-54-g00ecf