如何判断一个数组对象是数组对象的几种方法

it2025-12-26  12

利用Object的tostring方法来判断,原理是调用这个对象下的方法可以输出[object 构造函数],具体用法如下:

Obejct.prototype.tostring.call(需要判端的对象)

使用instance,instance的作用就是判断左边对象是不是等于右边对象,用法如下:var arr=[];console.lof(arr instanceof Array)

使用ES6新增的数组方法:console.log(Array.isArray(arr));

使用constructor方法,利用实例化对象与构造函数之间的联系判断:console.log(arr.constructor.name=='Array')

使用数组的下的push方法来判断console.log(arr.push!=undefined)

转载于:https://www.cnblogs.com/AngliaXu/p/7291130.html

最新回复(0)