Skip to content

区别数组和对象

instanceof

js
arr instanceof Array; // true
arr instanceof Object; // true
obj instanceof Array; // false

function exam(demo){
    let flat = false;
    if( (flat = demo instanceof Array) || demo instanceof Object ){
        if(flat === true) return 'Array';
        else return 'Object'
    }
}

construcor

js
arr.constructor === Array; // true
obj.constructor === Object; // true

Object.prototype.toString

js
Object.prototype.toString.call(arr) === '[object Array]'; // true
Object.prototype.toString.call(obj) === '[object Object]'; // true

isArray

js
Array.isArray(arr); // true
Array.isArray(obj); // false