JS中区分对象和数组的四种方法

方法一:经过调用constructor来识别ui

{}.constructor    //返回object
[].constructor    //返回Array
复制代码

方法二:经过instance of来识别spa

[] instance of Array   //true
{} instance of Array   //false
复制代码

方法三:经过Object,prototype.toString.call方法来识别prototype

Object.prototype.toString.call([])   //["object Array"]
Object.prototype.toString.call({})   //["object Object"]
复制代码

方法四:经过ES6中的Array.isArray来识别code

Array.isArray([])  //true
Array.isArray({})  //false
复制代码
相关文章
相关标签/搜索