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

Use instanceof for custom types:

 var ClassFirst = function () {};
var ClassSecond = function () {};
var instance = new ClassFirst();
typeof instance; // object
typeof instance == 'ClassFirst'; //obviously this one is false
instance instanceof Object; //true
instance instanceof ClassFirst; //true
instance instanceof ClassSecond; //false

Use typeof for simple built in types:

 'example string' instanceof String; // false
typeof 'example string' == 'string'; //true
'example string' instanceof Object; //false
typeof 'example string' == 'object'; //false
true instanceof Boolean; // false
typeof true == 'boolean'; //true
true instanceof Object; // false
99.99 instanceof Number; // false
typeof 99.99 == 'number'; //true
function() {} instanceof Function; //true
typeof function() {}; //function

Use instanceof for complex built in types:

/regularexpression/ instanceof RegExp; // true
typeof /regularexpression/; //object
[] instanceof Array; // true
typeof []; //object
{} instanceof Object; // true
typeof {}; //object

And the last one is a little bit tricki:

typeof null; //object

typeof instanceof的更多相关文章

  1. 小tip:关于typeof,instanceof,toString(),valueOf(),toLocaleString(),join(),reverse(),sort(),pop(),push(),shift(),unshift()

    typeof:用于检测一个变量是否是基本数据类型.instanceof用于检测某引用对象是什么类型的对象. var s = "Nicho"; var b = true; var n ...

  2. valueOf() toString() typeof instanceof

    ******在chrome console中运行{a:1}.valueOf(); 报错:"SyntaxError: Unexpected token . ",这是由于{}被js引擎 ...

  3. typeof instanceof 之间的区别总结

        typeof   它返回值是一个字符串,该字符串说明运算数的类型. a=1; b=true; c="c"; d=function(){ console.log(" ...

  4. Javascript中typeof instanceof constructor的区别

    typeof typeof,是一个运算符,运算中需要一个操作数,运算的结果就是这个操作数的类型,运算的结果是一个字符串.他有一定的局限性,对于对象类型的值,只能得到一个object结果,却不能精确得到 ...

  5. 【Flex教程】#009 As/typeof /instanceof /is的作用

    “as” :主要用它做类型转化 假设有一个类叫做Class1,我们声明了一个它的对象 c1,如果想要将它转换成Class2类型,只要这样写: Class2(c1); AS3 中的操作符: as 实现就 ...

  6. 推断js中的类型:typeof / instanceof / constructor / prototype

    怎样推断js中的类型呢,先举几个样例: var a = "jason"; var b = 123; var c = true; var d = [1,2,3]; var e = n ...

  7. typeof + instanceof+toString+constructor什么推理javascript数据类型

    一个.typeof JS这些变量是弱类型(这是弱类型)的,它可以不管用来存储数据的类型的. typeof 数据类型可用于检测给定的变量.可能的返回值: 1. 'undefined' --- 这个值没有 ...

  8. JavaScript的三种类型检测typeof , instanceof , toString比较

    1.typeof typeof是js的一个操作符,在类型检测中,几乎没有任何用处. typeof 返回一个表达式的数据类型的字符串,返回结果为javascript中的基本数据类型,包括:number. ...

  9. JS中 typeof,instanceof类型检测方式

    在js中的类型检测目前我所知道的是三种方式,分别有它们的应用场景: 1.typeof:主要用于检测基本类型. typeof undefined;//=> undefined typeof 'a' ...

随机推荐

  1. Spring的beans标签下可以有其他标签

    以前有对xsd(也就是schema文件)小做研究,有个小困惑,就是我们定义的元素只能使用定义的哪一些标签,比如<beans>下面就只能有自定义的哪一些,那为什么在引入<context ...

  2. Hibernate,JPA注解@EmbeddedId

    定义组合主键的几种语法: 将组件类注解为@Embeddable,并将组件的属性注解为@Id 将组件的属性注解为@EmbeddedId 将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为 ...

  3. MySQL start and stop

    一.本文说明 本实验主要是演示MySQL的四种启动方式,附带停止的操作. 二.mysqld mysqld is the MySQL server   mysqld reads options from ...

  4. Python使用报错记录

    问题1:pip 报错 C:\Users\Administrator>pip3 install pyreadline Fatal error in launcher: Unable to crea ...

  5. 微信利用PHP创建自定义菜单的方法

    在使用通用接口前,你需要做以下两步工作:1.拥有一个微信公众账号,并获取到appid和appsecret(在公众平台申请内测资格,审核通过后可获得)2.通过获取凭证接口获取到access_token注 ...

  6. PostgreSQL中如何查看一个表所对应的文件

    通过pg_relation_filepath可以直接表(索引)对象对应的物理文件在哪里? 上面截图是“德哥”做的ppt:上面有详细解释! 当然也可以通过 系统表 pg_class 可以直接查出对应的物 ...

  7. javascript 中==和===的区别

        对于JavaScript中比较运算符,可能大家用的比较多的是“==”.对于“===”很多人可能很陌生.=== 表示恒等,首先比较两边的变量数据类型是否相等,其次比较两边的变量的数值是否相等:= ...

  8. ACM题目————士兵杀敌(三)

    [RMQ算法]:用于当数组过于庞大的时候,查询区间的最大(最小)值. 时间复杂度:O(nlogn),主要时间发费在预处理上,查询只要O(1). 描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军 ...

  9. RTC系统

    http://blog.csdn.net/fanqipin/article/details/8089995 一. RTC及驱动简介 RTC即real time clock实时时钟,主要用于为操作系统提 ...

  10. Paths on a Grid(简单组合数学)

    Paths on a Grid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 23008 Accepted: 5683 Desc ...