1 、typeof
typeof '1' // string
typeof 1 // number
typeof false // boolean
typeof undefined // undefined
function f() {}
typeof f // function
typeof {} // object
typeof [] // object
typeof null // object
typeof window // object
// ***typeof 还能判断未声明的变量且不报错
v // ReferenceError: v is not defined
type v // undefined
2、instanceof
// 判断a是否为b的实例, 返回true/false
a instanceof b
[] instanceof Array // true
{} instanceof Object // true
function a() {}
a instanceof Function // true
false instanceof Boolean // false
true instanceof Boolean // false
1 instanceof Number // false
undefined instanceof undefined // Uncaught TypeError: Right-hand side of 'instanceof' is not an object
...
null instanceof Object // false
// 基本数据类型使用instanceof判断基本都返回false
3、Object.prototype.toString
// 使用Object.prototype.toString.call 或者 Object.prototype.toString.apply 都能准确的将基本数据、引用数据判断出来,返回 '[object 数据类型]'
Object.prototype.toString.call([]) // '[object Array]'
Object.prototype.toString.call(null) // '[object Null]'
Object.prototype.toString.call(undefined) // '[object Undefined]'
Object.prototype.toString.call(false) // '[object Boolean]'
....