1,typeof
typeof用于检测除了null以外的数据类型
typeof undefined // "undefined"
typeof null // "object"
typeof 1 // "number"
typeof "1" // "string"
typeof Symbol() // "symbol"
typeof function() {} // "function"
typeof {} // "object"
2,instanceof
用于判断复杂数据类型是否是对象,是就返回true,不是就返回false
Version:0.9 StartHTML:0000000105 EndHTML:0000001715 StartFragment:0000000141 EndFragment:0000001675
instanceof 不能用于判断原始数据类型的数据:
3 instanceof Number // false
'3' instanceof String // false
true instanceof Boolean // false
instanceof 可以用来判断对象的类型:
var date = new Date()
date instanceof Date // true
var number = new Number()
number instanceof Number // true
var string = new String()
string instanceof String // true
3,Object.prototype.toString.call()
可以用于检测所有数据类型,返回结果为'[object Number]',后面那个元素就表示他的类型
Object.prototype.toString.call(undefined) // "Undefined"
Object.prototype.toString.call(null) // "Null"
Object.prototype.toString.call(3) // "Number"
Object.prototype.toString.call(new Number(3)) // "Number"
Object.prototype.toString.call(true) // "Boolean"
Object.prototype.toString.call('3') // "String"
Object.prototype.toString.call(Symbol()) // "Symbol"
由上面的示例可知,该方法没有办法区分数字类型和数字对象类型,同理还不能区分字符串类型和字符转对象类型