对于判断参数类型
typeof 这个估计是最常用的了 , 比如下面的两个简单的例子
typeof {} // 'object'
typeof /\d/ // 'object'
typeof null // 'object'
typeof undefined // "undefined"
但是,大家上面的例子也看到了,如果我们想具体区分参数到底是 Object 对象还是 null ,使用typeof 是不行的,因为很多类型的返回都是 ‘object’.
大家可以试试下面的方法:
Object.prototype.toString.call(/\d/) // "[object RegExp]"
Object.prototype.toString.call(null) // "[object Null]"
var d = null;
d === Object( d ); // false
var d = { a:1 }
d === Object( d ); // true
var d = /\d/
d === Object( d ); // true