一、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]
本文介绍了JavaScript中使用typeof和instanceof进行类型检测的方法,并展示了如何利用Object.prototype.toString.call()准确判断各种数据类型,包括基本类型和复杂对象。
1491

被折叠的 条评论
为什么被折叠?



