检测数据类型 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) {};

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 查看所有版本


暂无话题~