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. 【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.4.Tabs控件

    之前,我们已经介绍了 jQuery UI 库,CSS 框架.下面,我们将学习这些有增强可视化效果,高度可配置的用户交互组件. Tab 的特性是,点击 tab 后,会高亮该 tab,并显示他的关联con ...

  2. tr DEMO

    测试数据: [weblogic@etp-mall-dev7][/tmp]$ cat msn.txt aaa bbb bbb ccc ccc ddd bbb eee aaa ccc bbb sss 转换 ...

  3. 创建sh文件

    创建sh文件 #/bin/bash v_file=$ v_type=$ v_desc=$ touch $v_file echo '#================================== ...

  4. ios tabbar 文字位置

    [nav.tabBarItem setTitlePositionAdjustment)];

  5. ajax 无刷新分页

    //ajax 无刷新分页1.前台要做的 滑动时 当前page+1,通过page ajax请求后台接口获取数据将数据进行拼装;2.后台要做的 做分页接口返回json数据前台判断触发请求条件: var p ...

  6. 查找练习 hash——出现过的数字 分类: 查找 2015-06-18 17:30 7人阅读 评论(0) 收藏

    查找练习 hash--出现过的数字 *Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 有一个数据字典,里面存有n个数字(n<=10 ...

  7. setprecision(int n)等格式函数用法 分类: POJ 2015-06-11 10:56 17人阅读 评论(0) 收藏

    **这些用法前最好用 #include <iostream>    //不要用iostream.h ,会出现好多问题 #include <iomanip> // io 流控制头 ...

  8. 分页sql

    /// <summary> /// 根据页数分页 /// </summary> /// <param name="page"></para ...

  9. Epoll,Poll,Select模型比较

    http://blog.csdn.net/liangyuannao/article/details/7776057 先说Select: 1.Socket数量限制:该模式可操作的Socket数由FD_S ...

  10. Spring 中的页面重定向

    在写 java 程序设计实践作业的时候遇到了重定向页面的需求,因为还没学到 java web 开发,所以自己就在网上搜了一下相关的代码,总结出了一些小小的经验,希望在下学期学 java web 的时候 ...