首页 > 其他 > 详细

typeof instanceof

时间:2014-01-22 22:27:50      阅读:455      评论:0      收藏:0      [点我收藏+]


typeof用以获取一个变量的类型,typeof一般只能返回如下几个结果:number,boolean,string,function,object,undefined
instanceof用于判断一个变量是否某个对象的实例

Use instanceof for custom types:

bubuko.com,布布扣
1 var ClassFirst = function () {};
2 var ClassSecond = function () {};
3 var instance = new ClassFirst();
4 typeof instance; // object
5 typeof instance == ‘ClassFirst‘; //obviously this one is false
6 instance instanceof Object; //true
7 instance instanceof ClassFirst; //true
8 instance instanceof ClassSecond; //false 
bubuko.com,布布扣

Use typeof for simple built in types:

bubuko.com,布布扣
 1 ‘example string‘ instanceof String; // false
 2 typeof ‘example string‘ == ‘string‘; //true
 3 ‘example string‘ instanceof Object; //false
 4 typeof ‘example string‘ == ‘object‘; //false
 5 true instanceof Boolean; // false
 6 typeof true == ‘boolean‘; //true
 7 true instanceof Object; // false
 8 99.99 instanceof Number; // false
 9 typeof 99.99 == ‘number‘; //true
10 function() {} instanceof Function; //true
11 typeof function() {}; //function
bubuko.com,布布扣

Use instanceof for complex built in types:

bubuko.com,布布扣
/regularexpression/ instanceof RegExp; // true
typeof /regularexpression/; //object
[] instanceof Array; // true
typeof []; //object
{} instanceof Object; // true
typeof {}; //object
bubuko.com,布布扣

And the last one is a little bit tricki:

typeof null; //object

typeof instanceof

原文:http://www.cnblogs.com/kite-Runner/p/3529907.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!