午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

js 數(shù)據(jù)類型判斷的方法

瀏覽:177日期:2024-04-11 08:31:06

typeof

一般用于判斷基本數(shù)據(jù)類型,用于判斷引用數(shù)據(jù)類型和null時(shí)會(huì)發(fā)生意外的錯(cuò)誤

typeof 1 // numbertypeof ’1’ // stringtypeof true // booleantypeof Symbol(’1’) // symboltypeof undefined // undefinedtypeof function(){} // functiontypeof { a: 1 } // objecttypeof [1, 2, 3] // object 這里會(huì)判斷異常,建議使用Array.isArray區(qū)分?jǐn)?shù)組和對(duì)象//以下也會(huì)判斷異常typeof new Boolean(true) === ’object’;typeof new Number(1) === ’object’;typeof new String(’abc’) === ’object’;//最后來看nulltypeof null // object

來看下typeof的原理:不同的對(duì)象在底層都表示為二進(jìn)制,在js里二進(jìn)制前三位都為0的會(huì) 被判斷為object類型,null的二進(jìn)制表示全0(對(duì)應(yīng)機(jī)器碼的null指針,一般為全0),所以會(huì)被判斷成object類型。

instanceof

它的主要作用是用來判斷一個(gè)實(shí)例是否屬于某種類型,用于判斷對(duì)象很合適

語法:object instanceof constructor object 某個(gè)實(shí)例對(duì)象 constructor 某個(gè)構(gòu)造函數(shù)’abc’ instanceof String //false 檢查原型鏈會(huì)返回undefinednew String(’abc’) instanceof String //truenew Boolean(true) instanceof Boolean // true new Number(1) instanceof Number // true順便做一下簡單實(shí)現(xiàn)function new_instance_of(leftVaule, rightVaule) { let leftProto = leftVaule.__proto__; // 取左表達(dá)式的__proto__值 let rightPrototype = rightVaule.prototype; // 取右表達(dá)式的 prototype 值 while (true) { if (leftProto === null) { return false; } if (rightPrototype === rightProto) { return true; } leftProto = leftProto.__proto__ }}

constructor

根據(jù)數(shù)據(jù)類型的構(gòu)造函數(shù)返回類型,但是由于null和undefined沒有構(gòu)造函數(shù)故無法判斷

’’.constructor == String //true new Number(1).constructor == Number //true new Function().constructor == Function //true true.constructor == Boolean //truenew Date().constructor == Date //true

Object.prototype.toString.call()

可以通過 toString() 來獲取每個(gè)對(duì)象的類型。為了每個(gè)對(duì)象都能通過Object.prototype.toString() 來檢測,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式來調(diào)用,傳遞要檢查的對(duì)象作為第一個(gè)參數(shù)。

var toString = Object.prototype.toString;toString.call(new Date); // [object Date]toString.call(new String); // [object String]toString.call(Math); // [object Math]toString.call(undefined); // [object Undefined]toString.call(null); // [object Null]

lodash.getTag和lodash.baseGetTag

baseGetTag使用Object.prototype.toString.call和Symbol.toStringTag來判斷屬性的類型Symbol.toStringTag只適合做特定的類型判斷

js 數(shù)據(jù)類型判斷的方法

//lodash.baseGetTag部分重要源碼//如果值是undefined和null返回對(duì)應(yīng)tag if (value == null) { return value === undefined ? ’[object Undefined]’ : ’[object Null]’ } // 如果不支持Symbol或者value值上面沒有Symbol.toStringTag屬性, //直接返回Object.prototype.toString調(diào)用后的值 if (!(symToStringTag && symToStringTag in Object(value))) { return toString.call(value) }

以上就是js 數(shù)據(jù)類型判斷的方法的詳細(xì)內(nèi)容,更多關(guān)于js 數(shù)據(jù)類型判斷的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 国产免费a视频 | 午夜影院在线观看视频 | 在线播放亚洲 | 精品1卡二卡三卡四卡老狼 亚洲网在线观看 | 福利视频在线导航 | 欧美日韩国产精品 | 国产黄色免费 | 超碰最新网址 | 奇米影视中文字幕 | 中国女人一级一次看片 | 在线a网 | 日韩欧美视频免费观看 | 三级黄毛片 | 欧美一区二区精品 | 大地资源在线资源 | 91香蕉国产视频 | 国精产品99永久一区一区 | 超碰97人人在线 | 成人小视频在线观看 | 色先锋在线 | 欧美一级免费 | 成 年人 黄 色 片 | 成人自拍网 | 国产视频你懂的 | 最新av中文字幕 | 91资源在线观看 | 香蕉视频911| 久久成人激情 | 粗大黑人巨茎大战欧美成人免费看 | 久草成人在线视频 | 久久91久久 | 日韩欧美高清在线 | 免费成人在线视频观看 | 爱草在线视频 | 国产在线视频在线观看 | 久在线观看 | 91精品久久香蕉国产线看观看 | 国产日韩欧美激情 | 不卡av一区 | 国产女人高潮时对白 | 欧美成人精品欧美一级私黄 |