typeof和instanceof

227 阅读1分钟

1、typeof

判断基本数据类型时,用typeof

(有一个例外:typeof null === "object")

// 基本数据类型
typeof 11 // 'number'
typeof '11' // 'string'
typeof true // 'boolean'
typeof undefined // 'undefined'
typeof null // 'object'
// 引用类型
typeof [1,2,3] // 'object'
typeof {a: 1, b: 2} // 'object'
typeof function() {} // 'function'

2、instanceof

主要的作用就是判断一个实例是否属于某种类型,也可判断一个实例是否是其父类型或者祖先类型的实例

let person = function () {
}
let nicole = new person()
nicole instanceof person // true