--- title: Array.isArray() slug: Web/JavaScript/Reference/Global_Objects/Array/isArray tags: - ECMAScript5 - JavaScript - 'brush: js' - class= - polyfill - 数组 - 方法 translation_of: Web/JavaScript/Reference/Global_Objects/Array/isArray --- <div>{{JSRef}}</div> <p><strong>Array.isArray() </strong>用于确定传递的值是否是一个 {{jsxref("Array")}}。</p> <pre class="brush: js">Array.isArray([1, 2, 3]); // true Array.isArray({foo: 123}); // false Array.isArray("foobar"); // false Array.isArray(undefined); // false </pre> <h2 id="语法">语法</h2> <pre>Array.isArray(<var>obj</var>)</pre> <h3 id="参数">参数</h3> <dl> <dt><code>obj</code></dt> <dd>需要检测的值。</dd> </dl> <h3 id="返回值">返回值</h3> <p>如果值是 {{jsxref("Array")}},则为true; 否则为false。</p> <h2 id="描述">描述</h2> <p>如果对象是 {{jsxref("Array")}} ,则返回true,否则为false。</p> <p>有关更多详细信息,请参阅文章<a href="http://web.mit.edu/jwalden/www/isArray.html">严格判定JavaScript对象是否为数组</a>。</p> <p>See the article <a href="http://web.mit.edu/jwalden/www/isArray.html">“Determining with absolute accuracy whether or not a JavaScript object is an array”</a> for more details. Given a {{jsxref("TypedArray")}} instance, <code>false</code> is always returned.</p> <h2 id="示例">示例</h2> <pre class="brush: js">// 下面的函数调用都返回 true Array.isArray([]); Array.isArray([1]); Array.isArray(new Array()); Array.isArray(new Array('a', 'b', 'c', 'd')) // 鲜为人知的事实:其实 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(new Uint8Array(32)) Array.isArray({ __proto__: Array.prototype }); </pre> <h3 id="instanceof_和_isArray"><code>instanceof</code> 和 <code>isArray</code></h3> <p>当检测Array实例时, <code>Array.isArray</code> 优于 <code>instanceof</code>,因为Array.isArray能检测<code>iframes</code>.</p> <pre><code>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] // Correctly checking for Array Array.isArray(arr); // true // Considered harmful, because doesn't work though iframes arr instanceof Array; // false</code></pre> <h2 id="Polyfill">Polyfill</h2> <p>假如不存在 Array.isArray(),则在其他代码之前运行下面的代码将创建该方法。</p> <pre class="brush: js">if (!Array.isArray) { Array.isArray = function(arg) { return Object.prototype.toString.call(arg) === '[object Array]'; }; } </pre> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('ES5.1', '#sec-15.4.3.2', 'Array.isArray')}}</td> <td>{{Spec2('ES5.1')}}</td> <td>Initial definition. Implemented in JavaScript 1.8.5.</td> </tr> <tr> <td>{{SpecName('ES6', '#sec-array.isarray', 'Array.isArray')}}</td> <td>{{Spec2('ES6')}}</td> <td></td> </tr> <tr> <td>{{SpecName('ESDraft', '#sec-array.isarray', 'Array.isArray')}}</td> <td>{{Spec2('ESDraft')}}</td> <td></td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <div> <div> <p>{{Compat("javascript.builtins.Array.isArray")}}</p> </div> </div> <h2 id="相关链接">相关链接</h2> <ul> <li>{{jsxref("Array")}}</li> </ul>