欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js怎么判斷是否是數(shù)組的六種方法小結(jié)

 更新時間:2023年02月10日 09:37:12   作者:下雨打傘干嘛  
本文主要介紹了js怎么判斷是否是數(shù)組的六種方法小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

instanceof

主要用來判斷某個實例是否屬于某個對象所在的原型鏈上,因此并不能完全分辨出到底是否是數(shù)組

  let a = [1, 2, 3];
      console.log(a instanceof Array); // true
      console.log(a instanceof Object); // true
      //從此我們可以看出a既是數(shù)組,也是對象
 
  let userInfo = { userName: "zhangsan" };
      console.log(userInfo instanceof Array); // false
      console.log(userInfo instanceof Object); // true
      //userInfo只是對象,而不是數(shù)組

Array.isArray()

Array.isArray([1,2]); // true
Array.isArray({name:'zs'}); // false

constructor構(gòu)造函數(shù)

let a = [1,2];
a.__proto__.constructor === Array // true
a.__proto__.constructor === Object // false

a.constructor === Array // true
a.constructor === Object // false

toString

Object.prototype.toString.call([1,2]) // '[object Array]'
Object.prototype.toString.call({name:'zs'}) // '[object Object]'

isPrototypeOf

Array.prototype.isPrototypeOf([1,2]) // true
Array.prototype.isPrototypeOf({name:'zs'})  // false

getPrototypeOf

Object.getPrototypeOf([1,2]) === Array.prototype // true
Object.getPrototypeOf({name:'zs'}) === Array.prototype // false

到此這篇關(guān)于js怎么判斷是否是數(shù)組的六種方法小結(jié)的文章就介紹到這了,更多相關(guān)js 判斷是否是數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論