js中typeof 与 instanceof 的区别

liyong_301 2008-05-15 01:19:27
js中typeof 与 instanceof 的区别?
...全文
2230 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wenhaoz 2011-07-20
  • 打赏
  • 举报
回复
怎么不结贴。。。。
莱登堡 2009-02-11
  • 打赏
  • 举报
回复
Javascript 的 typeof可以获取变量的类型
有如下6种返回值:
1)number;
2)string;
3)boolean;
4)object;
5)function;
6)undefined.

如下:(大家可以粘贴之后保存为.html文件看看效果)
<html>
<head>
<title>运算符typeof的功能和用法演示</title>

</head>
<body>
<p>
<Script Language = "JavaScript">
<!--
document.write("88的数据类型是:"+ typeof(88),"<br>");
document.write("古国的数据类型是:"+ typeof('古国'),"<br>");
document.write("true的数据类型是:"+ typeof(true),"<br>");
document.write("false的数据类型是:"+ typeof(false),"<br>");
// -->
</Script>
<br><br>
另外:<br>
对象类型返回的值为object<br>
函数类型返回的值是:function<br>
如果没有定义一个变量typeof对这个变量的操作结果是undefined<br>
</body></html>
午夜咖啡男 2009-02-11
  • 打赏
  • 举报
回复
简单来说呢,typeof 返回一个东西属于6种js基本类型的哪一种,instanceof 用来检测6中基本类型之一Object是不是某一个构造方法产生的实例,回溯原型链。
tantaiyizu 2009-02-11
  • 打赏
  • 举报
回复
dh20156 2009-02-11
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 muxrwc 的回复:]
貌似typeof 不止6种返回类型吧 - -
JScript code测试文件名为
demo.htm<script type="text/javascript">vara=newwindow.ActiveXObject('Microsoft.XMLHTTP');
a.open('get','demo.htm',false);
a.send(null);
alert(typeofa.responseBody);//unknown</script>
[/Quote]

^_^
rjzou2006 2009-02-11
  • 打赏
  • 举报
回复
typeof return a data type .
instanceof return a boolen for object is instanced .

muxrwc 2008-05-15
  • 打赏
  • 举报
回复
貌似typeof 不止6种返回类型吧 - -

测试文件名为
demo.htm
<script type="text/javascript">
var a = new window.ActiveXObject('Microsoft.XMLHTTP');
a.open('get', 'demo.htm', false);
a.send(null);
alert(typeof a.responseBody); //unknown
</script>
muxrwc 2008-05-15
  • 打赏
  • 举报
回复
instanceof
实际上就是检测实例是否有继承某类的原型链.
<script type="text/javascript">
var a = function () {};
var b = function () {};
b.prototype = new a;
var c = new b;
var d = new a;
alert(c instanceof a); //true
alert(d instanceof a); //true
alert(a instanceof Function); //true
alert({} instanceof Function); //false
a.prototype = {}; //改变原型链
alert(c instanceof a); //false
alert(d instanceof a); //false
</script>
gzdiablo 2008-05-15
  • 打赏
  • 举报
回复
typeof效率比较高
instanceof 适用的范围很广
这2者 看情况使用
s_liangchao1s 2008-05-15
  • 打赏
  • 举报
回复

<script>
var str=new String("hello");
alert(typeof(str));//返回string(无论引用什么类型都返回的是object)
alert(str instanceof String);//返回true,明确对象特定类型
</script>

lawrendc 2008-05-15
  • 打赏
  • 举报
回复
typeof 返回值有六种可能: "number," "string," "boolean," "object," "function," 和 "undefined."

instanceof返回一个 Boolean 值,指出对象是否是特定类的一个实例


function Student(name,age){
this.name=name;
this.age=age;
}

function Man(name,age){
this.name=name;
this.age=age;
}

var stu=new Student("dc",24);
var man1=new Man("mm",34);

alert (stu instanceof Student); //返回true

alert(man1 instanceof Student); //返回false

alert(man1 instanceof Man); //返回true

alert (typeof(stu)); //返回object

alert(typeof(man1)); //返回 object


不知道你是否能看懂
tantaiyizu 2008-05-15
  • 打赏
  • 举报
回复
typeof return a data type .
instanceof return a boolen for object is instanced .

87,914

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧