标签:
JS中常用来判定变量类型的两个函数为 typeof 和 instanceof
typeof variables的结果有 number, boolean, string, function, object[null, Array, Obeject, String,Date.....], undefined
要注意的地方是 typeof null的结果为object
null和undefined并不是等价的
var tmp = null; // 虽然tmp是null,但我也定义了 var undef; //声明但没定义 alert(typeof tmp); // object alert(typeof undef); // undefined
所以判断一个值是不是null的时候直接用 tmp == null即可,切勿使用typeof,null是一种特殊的对象
// 标量 //
typeof 123; // number
typeof ‘string‘; // string
typeof true; // boolean
// 函数 //
typeof function(){}; //function
// 对象 //
typeof new String(‘string‘); // object
typeof null; //object
instanceof 可以判断非标量的类型
‘string‘ instanceof String; // false new String(‘string‘) instanceof String; // true
标签:
原文地址:http://my.oschina.net/sallency/blog/529901