码迷,mamicode.com
首页 > 其他好文 > 详细

typeof

时间:2014-08-18 01:30:53      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:io   strong   ar   cti   new   ef   on   type   

一、typeof

语法为 typeof data,可能返回number,boolean,string,object,undefined,function

typeof "John"                 // Returns string 
typeof 3.14                   // Returns number
typeof false                  // Returns boolean
typeof [1,2,3,4]              // Returns object
typeof {name:‘John‘, age:34}  // Returns object

typeof null                 // Returns object特殊值null被认为是一个空的对象指针

typeof function(){}    //Returns function

typeof /w/               //Returns object or function Safari5及之前版本、Chrome7及之前版本对正则表达式调用typeof会返回function,其他浏览器返回object

undefined值派生自null值,所以alert(null==undefined)//返回true;但是alert(null===undefined)//返回false

不需要把变量的值显式设置为undefined,但在保存对象的变量还没真正保存对象时,就应该明确地让该变量设置为null值。这有助于区分未初始化的变量和空对象指针。

二、instanceof

语法为 o instanceof A,用来判断是否是要检测对象的实例。

var a=new String("a");

a instanceof String    //Return true 注意String大小写,不能用字符字面量,如var a="a", a instanceof String  返回false

var b=new Boolean;

b instanceof String  //Return true 注意String大小写

var c=new Date();

c instanceof Date  //Return true

var d=uull;

d instanceof Null  //Return false

var e=undefined;

e instanceof Undefined  //Return false

var f=function(){}

f instanceof Function  //Return true

f instanceof Object  //Return true

var g=[1,2,3];

g instanceof Array   //Return true

function H(){ }

var h=new H();

h instanceof H   //Return true

typeof,布布扣,bubuko.com

typeof

标签:io   strong   ar   cti   new   ef   on   type   

原文地址:http://www.cnblogs.com/xjinza/p/3918606.html

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