instanceof与typeof 运算符
typeof 运算符
描述
返回一个用来表示表达式的数据类型的字符串。
语法
typeof [ ( ] expression [ ) ] ;
expression 参数是需要查找类型信息的任意表达式 。
说明
typeof 运算符把类型信息当作字符串返回。typeof 返回值有六种可能: "number," "string," "boolean," "object," "function," 和 "undefined."
typeof 语法中的圆括号是可选项。
- alert(typeof (5)); //<SPAN>number</SPAN>
- alert(typeof (true)); //<SPAN>boolean</SPAN>
- alert(typeof ("abc")); //<SPAN><SPAN>string</SPAN>
- /SPAN>
alert(typeof (5)); //numberalert(typeof (true)); //booleanalert(typeof ("abc")); //string
---------------------------------------------------------------------------------------------------
instanceof 运算符
描述
返回一个 Boolean 值,指出对象是否是特定类 的一个实例。
语法
result = object instanceof class
instanceof 运算符的语法组成部分如下:
部分 描述 result 任何变量 。 object 任何对象表达式 。 class 任何已定义的对象类。
说明
如果 object 是 class 的一个实例,则 instanceof 运算符返回 true 。如果 object 不是指定类的一个实例,或者 object 是 null ,则返回 false 。
下面的例子举例说明了 instanceof 运算符的用法:
function objTest(obj) {var i, t, s = ""; // 创建变量。t = new Array(); // 创建一个数组。t["Date"] = Date; // 充填数组。t["Object"] = Object;t["Array"] = Array;for (i in t){if (obj instanceof t[i]) // 检查 obj 的类。{s += "obj is an instance of " + i + "\n";}else {s += "obj is not an instance of " + i + "\n";}}return(s); // 返回字符串。 } var obj = new Date(); response.write(objTest(obj));
javascript中instanceof和typeof解释
typeof用以获取一个变量的类型,typeof一般只能返回如下几个结果:number,boolean,string,function,object,undefined。我们可以使用typeof来获取一个变量是否存在,如if(typeof a!="undefined"){},而不要去使用if(a)因为如果a不存在(未声明)则会出错,对于Array,Null等特殊对象使用typeof一律返回object,这正是typeof的局限性。
如果我们希望获取一个对象是否是数组,或判断某个变量是否是某个对象的实例则要选择使用instanceof。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)会返回true。
谈到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
摘自:http://www.scriptlover.com/post/332
typeof return a data type .
instanceof return a boolen for object is instanced .
==================================================
instanceof返回一个 Boolean 值,指出对象是否是特定类的一个实例
- JScript code
-
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
不知道你是否能看懂
====================================================
instanceof 适用的范围很广
这2者 看情况使用
摘自:http://topic.csdn.net/u/20080515/13/b23b3e0b-c247-41f6-816d-2df5d2719d93.html
转载于:https://www.cnblogs.com/qiantuwuliang/archive/2009/07/19/1526605.html
instanceof与typeof 运算符相关推荐
- instanceof和typeof
我摘别人的: 对于instanceof和typeof,以前偶尔的用到过,特别是typeof用到的相对更多一些,今日研究ext源码,很多地方都用到了instanceof,突然觉得他们两个有些相似但也应该 ...
- c语言中typeof,运算符关键字typeof的使用
运算符关键字typeof的使用 引导语:C语言是一种计算机程序设计语言,它既具有 高级语言的特点,又具有 汇编语言的特点.以下是小编整理的运算符关键字typeof的'使用,欢迎参考阅读! 用于获取类型 ...
- Javascript的原型链、instanceof与typeof
为什么80%的码农都做不了架构师?>>> 在Javascript里,一切对象(Object和Function)都有内部的属性_proto_,但只Object.prototype ...
- 浅谈 instanceof 和 typeof 的实现原理
typeof 实现原理 typeof 一般被用于判断一个变量的类型,我们可以利用 typeof 来判断number, string, object, boolean, function, undefi ...
- 继承(instanceof :比较运算符;不仅运行父类方法,也运行子类独有的方法)
/**/ class Animaldome{public void eat(){}public void sleep(){} }class Cat extends Animaldome{public ...
- 收藏:JavaScript
=================================================== Aptana--Javascript开发工具(IDE) Aptana使用入门 Aptana支持J ...
- typeof和instanceof 运算符
instanceof运算符与typeof运算符相似,都适用于检测数据类型,但是在具体细节方面还是不同的 使用typeof测试数据类型 typeof 12;//"number" ty ...
- JavaScript中instanceof运算符的用法以及和typeof的区别
instanceof : 判断一个对象是否为某一数据类型,或一个变量是否为一个对象的实例:返回boolean类型栗子①: var aColors = ["red", "g ...
- 【js】typeof与instanceof
typeof 运算符 返回一个用来表示表达式的数据类型的字符串. typeof[()expression[]] ; expression 参数是需要查找类型信息的任意表达式. 说明 typeof 运算 ...
最新文章
- Science 好文:强化学习之后,机器人学习瓶颈如何突破?
- 《盗墓笔记》使用的这套技术,让美术可以在场景中任意使用灯光
- 学习响应式BootStrap来写融职教育网站,Bootsrtap第五天页脚
- PL/SQL Developer使用技巧总结
- docker 查看容器_Docker介绍
- 十七、PHP框架Laravel学习笔记——模型的定义
- 变压器绕组降低邻近效应_了解高频变压器设计基础(2)
- 4种语义分割数据集Cityscapes上SOTA方法总结
- 跨境电商和独立站哪个好?
- top 100 liked Q (26-)
- jdbc mysql url写法_Springboot项目连接MySql写了一个bug你也可能遇到
- Java求100以内素数和
- 单片机C语言入门自学指南(前期准备)
- 小白也能看懂的华为交换机常用命令大全
- Python实现三维数据(x, y, z)的索引——即通过(x, y)的值索引z的值
- 【志强课堂】今天聊一聊文案有何作用?
- 《位置大数据隐私管理》—— 1.3 LBS中的个人隐私与挑战
- 数据库 - 逻辑结构设计
- c#-线程-取消架构-Task-简单实验
- css绘制自定义数据仪表盘