typeof

  使用方式:typeof a 或者 typeof (a) 返回一个string类型的值

  顾名思义,这货是检查类型的,输出的是一个string值,直接看下面的检测代码:

console.log(typeof 'i am a string');  // string
console.log(typeof true); // boolean
console.log(typeof 10); // number
console.log(typeof undefined);  // undefined
console.log(typeof function() {});  // function
console.log(typeof null); // object
console.log(typeof new String('i am a string')); // object
console.log(typeof new Boolean(true)); // object
console.log(typeof new Number(10)); // object
console.log(typeof [1, 2, 3, 4]); // object
console.log(typeof new Array(1, 2, 3, 4)); // object
console.log(typeof new Date()); // object
console.log(typeof new RegExp('hello world', 'g')); // object
console.log(typeof /hello/g); // object

  这货最大的短板就是对null和数组的检测不利(都只能返回object)!

  typeof除了能初步检测变量的类型外,还能判断一个变量是否为空(没赋值或者没声明):

var a;
if(typeof a === 'undefined') {
  console.log('hello'); // hello
}

if(a === undefined) {
  console.log('world')  // world
}

  有趣的是没赋值和没声明的元素经过typeof运算后得到的结果一样,都是“undefined”字符串(因为本来就都是typeof undefined):

var a;
console.log(typeof a === typeof b); // true

  这时该如何区别?可以用 in window 判断:

var a;
console.log('a' in window); // true
console.log('b' in window); // false

  相对于instanceof,typeof还是比较简单的~

instanceof

  使用方式:a instanceof b 返回一个boolean

  instanceof的英文解释是实例,它的作用是判断某个变量是否为某一对象的实例。看检测代码:

console.log('string' instanceof String);  // false
console.log(true instanceof Boolean); // false
console.log(10 instanceof Number);  // false
console.log(undefined instanceof Object); // false
console.log(null instanceof Object);  // false

console.log({} instanceof Object);  // true
console.log(new String('string') instanceof String); // true
console.log(new Boolean(true) instanceof Boolean); // true
console.log(new Number(10) instanceof Number); // true
console.log([] instanceof Array); // true
console.log(new Array() instanceof Array); // true
console.log(new Date() instanceof Date); // true
console.log(new RegExp('') instanceof RegExp); // true
console.log(/hello/ instanceof RegExp); // true

  我们可以看到,instanceof规定基本数据类型(比如'string'、true、10等)都不能算是对象的实例,而new的一般都算。

   instanceof的第一个值(即a instanceof b中的a)必须先进行过声明,不然报错;而typeof则可对未声明变量进行判断(返回"undefined")。

  

typeof和instanceof简介及用法的更多相关文章

  1. js中typeof与instanceof的不同用法

    typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果: number,boolean,string,function(函数),object(NULL,数组,对象),und ...

  2. JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式

    相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...

  3. javascript中typeof和instanceof用法的总结

    今天在看相应的javascript书籍时,遇到了typeof和instanceof的问题,一直不太懂,特地查资料总结如下: JavaScript 中 typeof 和 instanceof 常用来判断 ...

  4. typeof 和 instanceof apply与call简单用法以及判断数组的坑

    1 typeof 和 instanceof var array = [];平时如果判断一个对象是否为数组,可能你会用 typeof array,但是输出为“object”. typeof 一般只能返回 ...

  5. typeof()和instanceof()用法区别

    typeof()和instanceof()用法区别: 两者都是用来判断数据类型的 typeof()是能用来判断是不是属于五大类型Boolean,Number,String,Null,Undefined ...

  6. JS中typeof和instanceof用法区别

    typeof和instanceof都可以用来判断变量 1.typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果: number,boolean,string,functi ...

  7. 比较typeof与instanceof?

    相同点:JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的. typeof的定义和用法:返回值是一个字符串,用来说明变量的数据类型. 细节: ...

  8. JS中typeof和instanceof的用法和区别

    typeof和instanceof的区别 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的 instanceof的用法 instanceof返回的是一个布尔值 ...

  9. typeof、instanceof、hasOwnProperty()、isPrototypeOf()

    typeof 操作符 instanceof 操作符 hasOwnProperty()方法 isPrototypeOf()方法 1.typeof 用于获取变量的类型,一般只返回以下几个值:string, ...

随机推荐

  1. Effective Java 29 Consider typesafe heterogeneous containers

    When a class literal is passed among methods to communicate both compile-time and runtime type infor ...

  2. 线程本地存储TLS(Thread Local Storage)的原理和实现——分类和原理

    原文链接地址:http://www.cppblog.com/Tim/archive/2012/07/04/181018.html 本文为线程本地存储TLS系列之分类和原理. 一.TLS简述和分类 我们 ...

  3. CBarChart柱形图类

    在用VC做有关图表的时候,感觉不是那么方便,在codeproject找到一个柱形图的实用类,原文地址为:http://www.codeproject.com/KB/miscctrl/CBarChart ...

  4. 读书笔记——网络编程与开发技术(3)基于TCP/IP协议的网络编程相关知识

    TCP/IP协议:数据链路层,网络层,传输层,应用层. IP地址分为5类:A类.B类.C类.D类.E类. (A类.B类.C类是基本类,D类多用于多播传送,E类为保留类.) "*"表 ...

  5. 虚拟机ping不通主机,但是主机可以ping通虚拟机(转载)

    我在Windows7系统安装了虚拟机,通过虚拟机安装了Ubuntu13.04,我设置的主机与虚拟机的连接方式是桥接,安装好后,发现虚拟机ping不通主机,但是主机可以ping通虚拟机. 我的操作是:关 ...

  6. putExtra方法

    [开篇骂几句:fuck]1.扯淡intent.putExtra()怎么使用?2.胡说intent.putExtra(); [扯淡:其实你在问它怎么用的时候,你要明白,你知道不知道这是个什么东东,有必要 ...

  7. linux下对2个连通的串口读写遇到的问题

    想要分析下zmodem协议,搜索发现linux下的工具lrzsz是一个包含x,y,z modem传输的工具,下载其源码,下载.它可以借助各种串行的接口进行数据传输,比如串口,socket也可以,这点描 ...

  8. 【温故而知新-Javascript】比较 undefined 和 null 值

    JavaScript 中有两个特数值: undefined和null,在比较它们的时候需要留心.在读取未赋值的变量或试图读取对象没有的属性时得到的就是 undefined 值. <!DOCTYP ...

  9. 【ASP.NET 插件】zyUpload的HTML5上传插件

    个人能力有限,只能网上找图片批量上传插件,看到一个还不错的插件zyUpload ,可以用来上传文件,但没有.NET 版本,特修改了下用以批量上传图片,效果图如下: update:2016年3月8日 有 ...

  10. AOJ 740 求和

      链接:http://icpc.ahu.edu.cn/OJ/Problem.aspx?id=740   Description 对于正整数n,k,我们定义这样一个函数f,它满足如下规律f(n,k=1 ...