1. typeof (可准确判断原始类型,对引用类型判断不够精准)
typeof 'str' === 'string'; //string
typeof 1 === 'number'; //number
typeof undefined === 'undefined'; //undefined
typeof true === 'boolean'; //boolean
typeof function(){} === 'function'; //function
typeof {a: 0} === 'object'; //object
typeof [1, 2] === 'object'; //判断正确,但不能精准判断Array类型
typeof null === 'object' //判断正确,但不能精准判断Null类型
typeof new Date() === 'object'; //判断正确,但不能精准判断Date类型 (其他对象类似)
var n=1,
s='hello',
b=true,
un=undefined,
nu=null,
ob={},
ar=[],
fun=function () {};
console.log(n, '类型是:'+typeof n); // 1 typeof number
console.log(b, '类型是:'+typeof b); // true typeof boolean
console.log(un, '类型是:'+typeof un); // undefined typeof undefined
console.log(nu, '类型是:'+typeof nu); // null typeof object
console.log(ob, '类型是:'+typeof ob); // {} typeof object
console.log(ar, '类型是:'+typeof ar); // [] typeof object
console.log(fun, '类型是:'+typeof fun); // ƒ () {} typeof function
2. instanceof
console.log('str' instanceof String) // false //#1
console.log(new String('str') instanceof String) //true //#2
console.log(2 instanceof Number) // false //#3
console.log(new Number(2) instanceof Number) //true //#4
console.log(true instanceof Boolean) //false //#5
console.log(new Boolean(true) instanceof Boolean) //true //#6
console.log([] instanceof Array) //true //#7
console.log({} instanceof Object) //true //#8
console.log(function(){} instanceof Function) //true //#9
以上结果显示,直接的字面量值判断数据类型,只有引用数据类型(Array,Function,Object)被精准判断,其他(数值Number,布尔值Boolean,字符串String)字面值不能被instanceof精准判断。而instanceof 在MDN中的解释为:instanceof 运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性。其意思就是判断对象是否是某一数据类型(如Array)的实例。由 行#1 ~ 行#6 也可看出这个解释:字面量值'str',2, true 不是实例,所以判断值为false,而行#2、#4、#6显示 字面量值实例化后判断为true。
因此也就导致了 instanceof 用来判断 null 和 undefined 会出现问题:
3. constructor (返回对创建此对象的函数的引用)
(2).constructor === Number //true
(true).constructor === Boolean //true
('str').constructor === String //true
([]).constructor === Array //true
({}).constructor === Object //true
(function(){}).constructor === Function //true
从运行结果可以看出 constructor 对于类型的判断是比较精准的,但它仍然存在一个问题,当更改创建对象的原型时,会发现其类型判断也发生了改变:
4. Object.prototype.toString.call() (推荐使用,可准确判断对象类型)
Object.prototype.toString.call('str') === "[object String]"; //String
Object.prototype.toString.call(1) === "[object Number]"; //Number
Object.prototype.toString.call(true) === "[object Boolean]"; //Boolean
Object.prototype.toString.call(undefined) === "[object Undefined]"; //Undefined
Object.prototype.toString.call(null) === "[object Null]"; //Null
Object.prototype.toString.call({a: 0}) === "[object Object]"; //Object
Object.prototype.toString.call([0, 1, 2]) === "[object Array]"; //Array
Object.prototype.toString.call(new Date()) === "[object Date]"; //Date
Object.prototype.toString.call(() => {}) === "[object Function]" //Function
资料1. https://blog.csdn.net/zjy_android_blog/article/details/81023177
资料2. https://www.cnblogs.com/aoyo/p/5245162.html
资料3. https://blog.csdn.net/zhangjing0320/article/details/81230170
资料4. https://blog.csdn.net/u011272795/article/details/80105513