Podsumowanie
Array.isArray() to metoda, która zwraca true
jeśli obiekt jest tablicą, fałsz jeśli nie jest.
Składnia
Array.isArray(obj)
Parametry
obj
- Obiekt do sprawdzenia
Opis
Zobacz artykuł “Determining with absolute accuracy whether or not a JavaScript object is an array” , aby poznać więcej szczegółów.
Przykłady
// poniższe przykłady zwrócą true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
// Mało znany fakt: Array.prototype sam w sobie jest tablicą:
Array.isArray(Array.prototype);
// poniższe przykłady zwrócą 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 });
Dostępność wsteczna
Jeśli metody Array.isArray() nie jest natywnie dostępna, poniższy kod ją utworzy.
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
Specyfikacja
Specyfikacja | Status | Komentarz |
---|---|---|
ECMAScript 5.1 (ECMA-262) The definition of 'Array.isArray' in that specification. |
Standard | Wstępna definicja. Implementacja od JavaScript 1.8.5. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.isArray' in that specification. |
Standard |
Zgodność z przeglądarkami
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 5 | 4.0 (2.0) | 9 | 10.5 | 5 |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | 4.0 (2.0) | (Yes) | (Yes) | (Yes) |
Based on Kangax's compat table.