判斷變量是數組還是對象,使用Object.prototype.toString.call(),兼容性好,切勿使用typeof來判斷對象或者數組,因為typeof得到的都是object;
function isObjArr(value){ if (Object.prototype.toString.call(value) === "[object Array]") { console.log('value是數組'); }else if(Object.prototype.toString.call(value)==='[object Object]'){ console.log('value是對象'); }else{ console.log('value不是數組也不是對象') } }