关于javascript:判断是否为数组的-JavaScript-方法总结

3次阅读

共计 3349 个字符,预计需要花费 9 分钟才能阅读完成。

前言

咱们在日常开发中,经常有判断某值类型的需要,明天咱们总结一下常见的几种用来判断是否为数组的 JavaScript 办法。

Array.isArray

Array.isArray() 是 ES5 新增的办法,用于确定传递的值是否是一个数组,如果是数组,则返回 true,否则返回 false。

let arr = [];
console.log(Array.isArray(arr)); // true

上面的函数调用都返回 true:

Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
Array.isArray(new Array("a", "b", "c", "d"));

须要留神的一点是:其实 Array.prototype 也是一个数组。

Array.isArray(Array.prototype); // true

上面的函数调用都返回 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});

兼容性如下图:

能够看到,新版的支流浏览器都是反对该办法的,能够放心使用。

constructor

Object 的每个实例都有构造函数 constructor,用于保留着用于创立以后对象的函数

let arr = [];
console.log(arr.constructor === Array); // true

须要留神的是,constructor 有被批改的危险,判断后果不肯定精确,比方:

let arr = [1, 2, 3];
arr.constructor = function () {}
console.log(arr.constructor === Array); // false

个别不举荐应用 constructor 来判断是否为数组,咱们只须要晓得有这么一个办法就行。

instanceof

instanceof 运算符用于检测构造函数的 prototype 属性是否呈现在某个实例对象的原型链上。举个例子:

// 定义构造函数
function C() {}
function D() {}

var o = new C();

o instanceof C; // true,因为 Object.getPrototypeOf(o) === C.prototype

o instanceof D; // false,因为 D.prototype 不在 o 的原型链上

o instanceof Object; // true,因为 Object.prototype.isPrototypeOf(o) 返回 true
C.prototype instanceof Object; // true,同上

用 instanceof 来判断是否为数组的用法如下:

let arr = [];
console.log(arr instanceof Array); // true

应用 instanceof 须要留神两点:

  • 构造函数的 prototype 和 实例的原型链都有可能会扭转,所以判断出的后果不肯定变化无穷。
  • 在有 iframe 的页面脚本中应用 instanceof,可能会失去谬误的后果,因为 iframe 领有独立的全局环境,不同的全局环境领有不同的全局对象,从而领有不同的内置类型构造函数。

isPrototypeOf

isPrototypeOf() 能够用于测试一个对象是否存在于另一个对象的原型链上。用法如下:

function Foo() {}
function Bar() {}
function Baz() {}

Bar.prototype = Object.create(Foo.prototype);
Baz.prototype = Object.create(Bar.prototype);

var baz = new Baz();

console.log(Baz.prototype.isPrototypeOf(baz)); // true
console.log(Bar.prototype.isPrototypeOf(baz)); // true
console.log(Foo.prototype.isPrototypeOf(baz)); // true
console.log(Object.prototype.isPrototypeOf(baz)); // true

如果要用 isPrototypeOf 来判断传入参数是否为数组,能够这样用:

let arr = [];
console.log(Array.prototype.isPrototypeOf(arr)); // true

Object.prototype.toString

每个对象都有一个 toString() 办法,当该对象被示意为一个文本值时,或者一个对象以预期的字符串形式援用时主动调用。

默认状况下,toString() 办法被每个 Object 对象继承。如果此办法在自定义对象中未被笼罩,toString() 返回 “[object type]” 字符串,其中 type 是对象的类型。

能够通过 toString() 来获取每个对象的类型。为了每个对象都能通过 Object.prototype.toString() 来检测,须要以 Function.prototype.call() 或者 Function.prototype.apply() 的模式来调用,传递要查看的对象作为第一个参数,称为 thisArg。用法如下:

var toString = Object.prototype.toString;

toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math]

//Since JavaScript 1.8.5
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]

如果要用来判断一个对象是否为数组,可这样应用:

let arr = [];
console.log(Object.prototype.toString.call(arr) === "[object Array]"); // true

兼容性如下:

typeof

说到判断类型,可能很多人都会想到 typeof 办法,咱们这里来温习一下 typeof 相干内容。

typeof 操作符返回一个字符串,示意未经计算的操作数的类型。

console.log(typeof 42); // "number"
console.log(typeof 'blubber'); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undeclaredVariable); // "undefined"

typeof 可能的返回值如下:

通过上图能够看到,数组对象属于“其余任何对象”,所以数组对象的 typeof 返回值是“object”:

let arr = [];
console.log(typeof arr); // "object"

所以,咱们要尽量避免应用 typeof。

总结

以上就是几种用来判断一个值是否为数组的几种办法,当然有好用的也有不好用的,然而不管怎样,咱们晓得有这么回事总归是好的。总结一下:

  • 最好用的办法是 Array.isArray, 只是不反对 IE8 及以下。
  • 如果要思考兼容性,则能够应用 Object.prototype.toString

~

~ 本文完,感激浏览!

~

学习乏味的常识,结识乏味的敌人,塑造乏味的灵魂!

大家好,我是〖编程三昧〗的作者 隐逸王 ,我的公众号是『编程三昧』,欢送关注,心愿大家多多指教!

你来,怀揣冀望,我有墨香相迎!你归,无论得失,唯以余韵相赠!

常识与技能并重,内力和外功兼修,实践和实际两手都要抓、两手都要硬!

正文完
 0