Home > Web Front-end > JS Tutorial > javascript isArray() 判断某个值是否为数组

javascript isArray() 判断某个值是否为数组

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 09:54:29
Original
1603 people have browsed it

<strong>Array.isArray()</strong> 方法用来判断某个值是否为数组。如果是,则返回 true,否则返回 false

isArray语法

<code class="language-javascript">Array.isArray(value)</code>
Copy after login

 

isArray参数

参数 说明
value 需要检测的值。

 

isArray功能

<strong>isArray()</strong> 方法用来判断某个值是否为数组。如果是,则返回 true,否则返回 false

 

isArray实

<code class="language-javascript">// 下面的函数调用都返回 true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
// 鲜为人知的事实:其实 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 });</code>
Copy after login

 

isArray兼容性解决方法

假如不存在 Array.isArray(),则在其他代码之前运行下面的代码将创建该方法。

<code class="language-javascript">if (!Array.isArray) {
  Array.isArray = function(arg) {
    return Object.prototype.toString.call(arg) === '[object Array]';
  };
}</code>
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Where is js written?
From 1970-01-01 08:00:00
0
0
0
js addClass not working
From 1970-01-01 08:00:00
0
0
0
js file code not found
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template