码迷,mamicode.com
首页 > 编程语言 > 详细

typeof判断类型(数组类型得用instanceof)

时间:2016-10-22 20:40:20      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:style   number   方法   reg   date   null   包括   通过   define   

var a= 1;
console.log(typeof a); 
var b= ‘1‘;
console.log(typeof b); 
var c;
console.log(typeof c); 
var d= true;
console.log(typeof d); 
var e= [1,2,3];
console.log(typeof e); 
var f= function(){};
console.log(typeof f);
console.log(e instanceof Array)

观察输出结果发现,number, string, undefined, function,boolean类型均能通过typeof方法判断,

而array类型输出object,因为typeof方法只能判断基本类型类型(number, string, undefined,boolean),除此之外(包括Date, RegExp,null等都只是object的扩展!)都不靠谱

数组一般情况下可以用instanceof来判断。

typeof判断类型(数组类型得用instanceof)

标签:style   number   方法   reg   date   null   包括   通过   define   

原文地址:http://www.cnblogs.com/yanze/p/5988217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!