紫水晶编程技术论坛 - 努力打造成全国最好的编程论坛

 找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 5226|回复: 3

[JavaScript] JS中typeof与instanceof的区别

[复制链接]

1214

主题

1566

帖子

11

精华

管理员

菜鸟

Rank: 125Rank: 125Rank: 125Rank: 125Rank: 125

积分
93743

贡献奖关注奖人气王精英奖乐于助人勋章

发表于 2012-5-31 20:12:37 | 显示全部楼层 |阅读模式
JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的。但它们之间还是有区别的:

typeof

typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型。

它返回值是一个字符串,该字符串说明运算数的类型。typeof 一般只能返回如下几个结果:

number,boolean,string,function,object,undefined。我们可以使用 typeof 来获取一个变量是否存在,如 if(typeof a!="undefined"){alert("ok")},而不要去使用 if(a) 因为如果 a 不存在(未声明)则会出错,对于 Array,Null 等特殊对象使用 typeof 一律返回 object,这正是 typeof 的局限性。
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <script language="javascript" type="text/javascript">
  6. document.write ("typeof(1): "+typeof(1)+"<br>");
  7. document.write ("typeof(NaN): "+typeof(NaN)+"<br>");
  8. document.write ("typeof(Number.MIN_VALUE): "+typeof(Number.MIN_VALUE)+"<br>");
  9. document.write ("typeof(Infinity): "+typeof(Infinity)+"<br>");
  10. document.write ("typeof("123"): "+typeof("123")+"<br>");
  11. document.write ("typeof(true): "+typeof(true)+"<br>");
  12. document.write ("typeof(window): "+typeof(window)+"<br>");
  13. document.write ("typeof(Array()): "+typeof(new Array())+"<br>");
  14. document.write ("typeof(function(){}): "+typeof(function(){})+"<br>");
  15. document.write ("typeof(document): "+typeof(document)+"<br>");
  16. document.write ("typeof(null): "+typeof(null)+"<br>");
  17. document.write ("typeof(eval): "+typeof(eval)+"<br>");
  18. document.write ("typeof(Date): "+typeof(Date)+"<br>");
  19. document.write ("typeof(sss): "+typeof(sss)+"<br>");
  20. document.write ("typeof(undefined): "+typeof(undefined)+"<br>")
  21. </script>
  22. <title>javascript类型测试</title>
  23. </head>

  24. <body>
  25. </body>
  26. </html>
复制代码
instanceof

instance:实例,例子

a instanceof b?alert("true"):alert("false"); //a是b的实例?真:假

instanceof 用于判断一个变量是否某个对象的实例,如 var a=new Array();alert(a instanceof Array); 会返回 true,同时 alert(a instanceof Object) 也会返回 true;这是因为 Array 是 object 的子类。再如:function test(){};var a=new test();alert(a instanceof test) 会返回

谈到 instanceof 我们要多插入一个问题,就是 function 的 arguments,我们大家也许都认为 arguments 是一个 Array,但如果使用 instaceof 去测试会发现 arguments 不是一个 Array 对象,尽管看起来很像。

另外:

测试 var a=new Array();if (a instanceof Object) alert('Y');else alert('N');

得'Y’

但 if (window instanceof Object) alert('Y');else alert('N');

得'N'

所以,这里的 instanceof 测试的 object 是指 js 语法中的 object,不是指 dom 模型对象。

使用 typeof 会有些区别

alert(typeof(window)) 会得 object
【VB】QQ群:1422505加的请打上VB好友
【易语言】QQ群:9531809  或 177048
【FOXPRO】QQ群:6580324  或 33659603
【C/C++/VC】QQ群:3777552
【NiceBasic】QQ群:3703755

7

主题

421

帖子

1

精华

铂金会员

Rank: 5

积分
2173
发表于 2013-4-12 23:00:46 | 显示全部楼层
M,。。學習了

0

主题

68

帖子

0

精华

铜牌会员

Rank: 2Rank: 2

积分
94
发表于 2015-1-11 14:17:34 | 显示全部楼层
谢谢分享

30

主题

723

帖子

0

精华

钻石会员

Rank: 6Rank: 6

积分
2815
发表于 2015-5-22 23:01:24 | 显示全部楼层
感谢您的分享
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

手机版|Archiver|紫水晶工作室 ( 粤ICP备05020336号 )

GMT+8, 2024-4-27 21:38 , Processed in 0.023969 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表