typeof
一般用于判断基本数据类型,用于判断引用数据类型和null时会发生意外的错误
typeof 1 // number typeof '1' // string typeof true // boolean typeof Symbol('1') // symbol typeof undefined // undefined typeof function(){} // function typeof { a: 1 } // object typeof [1, 2, 3] // object 这里会判断异常,建议使用Array.isArray区分数组和对象 //以下也会判断异常 typeof new Boolean(true) === 'object'; typeof new Number(1) === 'object'; typeof new String('abc') === 'object'; //最后来看null typeof null // object
来看下typeof的原理:不同的对象在底层都表示为二进制,在js里二进制前三位都为0的会 被判断为object类型,null的二进制表示全0(对应机器码的null指针,一般为全0),所以会被判断成object类型。
instanceof
它的主要作用是用来判断一个实例是否属于某种类型,用于判断对象很合适
语法:object instanceof constructor object 某个实例对象 constructor 某个构造函数 'abc' instanceof String //false 检查原型链会返回undefined new String('abc') instanceof String //true new Boolean(true) instanceof Boolean // true new Number(1) instanceof Number // true 顺便做一下简单实现 function new_instance_of(leftVaule, rightVaule) { let leftProto = leftVaule.__proto__; // 取左表达式的__proto__值 let rightPrototype = rightVaule.prototype; // 取右表达式的 prototype 值 while (true) { if (leftProto === null) { return false; } if (rightPrototype === rightProto) { return true; } leftProto = leftProto.__proto__ } }
constructor
根据数据类型的构造函数返回类型,但是由于null和undefined没有构造函数故无法判断
''.constructor == String //true new Number(1).constructor == Number //true new Function().constructor == Function //true true.constructor == Boolean //true new Date().constructor == Date //true
Object.prototype.toString.call()
可以通过 toString() 来获取每个对象的类型。为了每个对象都能通过Object.prototype.toString() 来检测,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用,传递要检查的对象作为第一个参数。
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只适合做特定的类型判断
//lodash.baseGetTag部分重要源码 //如果值是undefined和null返回对应tag if (value == null) { return value === undefined ? '[object Undefined]' : '[object Null]' } // 如果不支持Symbol或者value值上面没有Symbol.toStringTag属性, //直接返回Object.prototype.toString调用后的值 if (!(symToStringTag && symToStringTag in Object(value))) { return toString.call(value) }
以上就是js 数据类型判断的方法的详细内容,更多关于js 数据类型判断的资料请关注其它相关文章!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“js 数据类型判断的方法”评论...
更新日志
2024年05月20日
2024年05月20日
- 张琍敏1978-雪中莲[台湾复刻版][WAV+CUE]
- 叶蕴仪1993-睡美人[日本版][WAV+CUE]
- 夜晚助兴音乐-群星《新时代床头音乐-性能量》2CD[WAV]
- 24K德国HD金碟《历届奥斯卡获奖金曲》3CD[WAV整轨]
- 邰正宵《重燃爱恋 贰 Walk On》[FLAC/分轨][431.72MB]
- 苏文劭《雨停出来走走》[320K/MP3][81.11MB]
- 苏文劭《雨停出来走走》[FLAC/分轨][210.76MB]
- 群星《2024好听新歌04》十倍音质WAV分轨
- 陈宁《弹指之间HQ》头版限量[低速原抓WAV+CUE]
- 陈宁《故人还》HQCDII限量签名版[低速原抓WAV+CUE]
- 苏文劭《春曲(Lessons)》[320K/MP3][39.8MB]
- 苏文劭《春曲(Lessons)》[FLAC/分轨][97.78MB]
- 群星《2006香港高级视听展原音精选 SACD》[ISO][2G]
- 张琍敏1977-枫林小雨[台湾复刻版][WAV+CUE]
- 林一峰2014-COOKINGMUSIC[香港首版][WAV+CUE]