检测数据类型 typeof
基本用法
typeof 1;
typeof(1); // "number"
基本类型
console.log(typeof null); //"object"
console.log(typeof undefined);//"undefined"
console.log(typeof 1);//"number"
console.log(typeof 'hello');//"string"
console.log(typeof true);//"boolean"
console.log(typeof {});//"object"
引用类型
Function
RegExp
Array
中只有 Function
使用 typeof
明确返回 "function"
Function
var callback = function() {}
typeof callback // "function"
if(typeof callback === "function") {};
RegExp
var reg = /^[0-9]*$/;
typeof reg // "object" 或 "function"
if (reg instanceof RegExp) {}
Array
var arr=["a","b"];
typeof arr // "object"
if (arr instanceof Array) {};