一,typeof
typeof 只适用于检测基本数据类型,而引用类型返回都是object,只有function返回function。
//js中一共六种数据类型
//5种基本类型 Undefined Null Boolean String Number
//一种复杂类型 Object
//type of 共会返回六种结果
//undrfined-- 如果这个值未定义
//boolean-- 如果这个值是布尔值
//string-- 如果这个值字符串
//number-- 如果这个值是数字
//object-- 如果这个值是对象或者null(null 被看成是一个空对象的引用)
//function-- 如果这个值是函数
var str= 'aa';//typeof---string
var num= 1;//typeof---number
var b= true;//typeof---boolean
var u;//typeof---undefined
//以上是基本类型
var func= function(){}//typeof---function
//以上是比较特殊的对象类型--函数类型
var n=null;//typeof---object
var o= new Object();//typeof---object
var arr= [1,2];//typeof---object
//以上是对象类型
//所以 typeof 只适合检测基本类型 和 函数
二,instanceof
instanceof运算符用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上
obj instanceof Object; //true 实例obj在不在Object构造函数中
三,严格模式检测
Object.prototype.toString.call()
打印示例







网友评论