typeof
一个一元运算符,放在运算数之前,运算数可以使任意数据类型。它返回的值是一个字符串,该字符串说明运算数的类型。typeof返回的结果是:number、boolean、string、function、object、undefined。对于array、null等特殊对象使用typeof一律返回object,这是typeof的局限性。
var a;typeof(a);//声不声明都返回undefined
不要使用if(a),因为如果a未声明会报错。
对于未声明过的变量,只能执行一项操作,即使用typeof操作符检测其数据类型
var age;
typeof(age); // undefined
typeof(name) //undefined
instanceof
用于判断一个变量是否是某个对象的实例,返回true或者false
这里的object指的是js语法中object,不是DOM模型对象,与typeof有区别,所以function test(){alert("a")};var a=new test();//相当于调用函数输出aalert(a instanceof test);//返回true
window instanceof Object//返回falsetypeof window//返回object
Object.prototype.toString.call(obj).slice(8,-1);
"123"--[object String];123--[object Number];[1,2,3]--[object Array];