js typeof和instanceof

一、typeof 检测返回的是对应的数据类型

console.log(typeof(123));//number
console.log(typeof(true));//boolean
console.log(typeof('33'));//string
console.log(typeof(null));//object

console.log(typeof([]));//object
console.log(typeof({}));//object
console.log(typeof(new Date()));//object

console.log(typeof(function(){}));//function
console.log(typeof(Array));//function


 

二、instanceof 检测 返回布尔值 true,false

instanceof 按原型链检测的

 

//Object.prototype.toString.call(str);来检测数据类型

Object.prototype.toString.call(1)//[object Number]
Object.prototype.toString.call("1")//[object String]
Object.prototype.toString.call([])//[object Array]
Object.prototype.toString.call({})//[object Object]
Object.prototype.toString.call(true)//[object Boolean]
Object.prototype.toString.call(null)[object Null]

 

 

Guess you like

Origin blog.csdn.net/lanwangxia/article/details/116986667