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

JavaScript类型检测

时间:2015-05-27 22:30:22      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

     在编写JS代码中,经常要对某个变量进行类型检测。常用的类型检测方法有:

  • typeof
  • Object.prototype.toString
  • constructor
  • instanceof

typeof

typeof可以识别出基本数据类型(null除外),同时typeof并不能识别具体的对象类型(Function除外).

eg:   

typeof "seven";     // "string"
typeof 7;     //"number"
typeof true ;     // "boolean"
typeof undefined;  // "undefined"
typeof null   ;       // "object"    
typeof {name:"seven"};   //"object"
typeof function(){};    //"function"
typeof [] ;    //"object"
typeof new Date;   //"object"
typeof /\w/ ;    // "object"
function Student(){};
typeof   new Student(); //"object"

  

 

JavaScript类型检测

标签:

原文地址:http://www.cnblogs.com/seven-zh/p/4534430.html

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