无意中瞟到一眼jQuery源码发现type方法的代码怎么看着那么闹腾,于是乎研究了一下,虽然不知道其中原理,但是从此妈妈再也不担心我如何判断数据类型了。。。。
以前认为用typeof判断就ok了,想想真是图样图森破啊,请看示例:
typeof new Boolean(1) = "object" typeof new Number(1) = "object" typeof new String('a') = "object" typeof new function(){return 1} = "object" typeof new Array('a','b') = "object" typeof new Date('Thu Jul 11 2013 11:51:31 GMT+0800(中国标准时间)') = "object" typeof new RegExp(/a/) = "object"
走到这突然发现尼玛怎么什么东西都是“object”,这不坑爹呢吗,最终翻来覆去研究jQuery源码曲奇精髓去其糟粕的总结出核心方法,请看示例:
Object.prototype.toString.call(new Boolean(1)) = "[object Boolean]" Object.prototype.toString.call(true) = "[object Boolean]" Object.prototype.toString.call(new Number(1)) = "[object Number]" Object.prototype.toString.call(100) = "[object Number]" Object.prototype.toString.call(new String("bbs0101")) = "[object String]" Object.prototype.toString.call("bbs0101") = "[object String]" Object.prototype.toString.call(function(){return 1}) = "[object Function]" Object.prototype.toString.call(new Array(1,2,3,4,5)) = "[object Array]" Object.prototype.toString.call([1,2,3,4,5]) = "[object Array]" Object.prototype.toString.call(new Date()) = "[object Date]" Object.prototype.toString.call(new RegExp('bbs0101')) = "[object RegExp]" Object.prototype.toString.call(/bbs0101/) = "[object Regexp]"
走到这突然感觉豁然开朗,然开朗,开朗,朗。。。。。