JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的。但它们之间还是有区别的:

typeof

typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型。

它返回值是一个字符串,该字符串说明运算数的类型。typeof 一般只能返回如下几个结果:

number,boolean,string,function,object,undefined。我们可以使用 typeof 来获取一个变量是否存在,如 if(typeof a!="undefined"){alert("ok")},而不要去使用 if(a) 因为如果 a 不存在(未声明)则会出错,对于 Array,Null 等特殊对象使用 typeof 一律返回 object,这正是 typeof 的局限性。

网上的一个小例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script language="javascript" type="text/javascript"> document.write ("typeof(1): "+typeof(1)+"<br>"); document.write ("typeof(NaN): "+typeof(NaN)+"<br>"); document.write ("typeof(Number.MIN_VALUE): "+typeof(Number.MIN_VALUE)+"<br>"); document.write ("typeof(Infinity): "+typeof(Infinity)+"<br>"); document.write ("typeof(\"123\"): "+typeof("123")+"<br>"); document.write ("typeof(true): "+typeof(true)+"<br>"); document.write ("typeof(window): "+typeof(window)+"<br>"); document.write ("typeof(Array()): "+typeof(new Array())+"<br>"); document.write ("typeof(function(){}): "+typeof(function(){})+"<br>"); document.write ("typeof(document): "+typeof(document)+"<br>"); document.write ("typeof(null): "+typeof(null)+"<br>"); document.write ("typeof(eval): "+typeof(eval)+"<br>"); document.write ("typeof(Date): "+typeof(Date)+"<br>"); document.write ("typeof(sss): "+typeof(sss)+"<br>"); document.write ("typeof(undefined): "+typeof(undefined)+"<br>") </script> <title>javascript类型测试</title> </head> <body> </body> </html>

instanceof

instance:实例,例子

a instanceof b?alert("true"):alert("false"); //a是b的实例?真:假

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) 会返回

谈到 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

转载于:https://www.cnblogs.com/kaka-bing/p/3154631.html

JS中typeof与instanceof的区别相关推荐

  1. 详解js中typeof、instanceof与constructor

    typeof返回一个表达式的数据类型的字符串,返回结果为js基本的数据类型,包括number,boolean,string,object,undefined,function.语法为typeof(da ...

  2. 小tips:JS中typeof与instanceof用法

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

  3. js中typeof与instanceof用法小记

    今天写JS代码,遇到动态生成多个名称相同的input复选按钮 需要判断其是否是数组,用到了if (typeof(document.MapCheckMgr.checkid)!="undefin ...

  4. JS中typeof的用法

    转载自  JS中typeof的用法 js是一门弱语言,它在声明变量时无需确定变量的类型,js在运行时会自动判断.那么如何判断一个变量的类型呢,js提供了typeof运算符,用来检测一个变量的类型. 1 ...

  5. Js中的style,currentStyle,getComputedStyle()区别

    Js中的style,currentStyle,getComputedStyle()区别  样式表有三种方式: 1.内嵌样式(inline Style)-是写在Tag里面的,内嵌样式只对所有的Tag有效 ...

  6. JS中 let 和var的区别

    JS中let和var 的区别 简单介绍let var的常见变量提升 ES6可以用let定义块级作用域变量 let配合for循环的独特应用 let没有变量提升与暂时性死区 let变量不能重复声明 简单介 ...

  7. uniapp 获取到js文件var一个变量怎么获取到这个变量值_浅析Js中const,let,var的区别及作用域...

    理解:let变量的作用域只能在当前函数中 js中const,let,var的区别及作用域_lianzhang861的博客-CSDN博客​blog.csdn.net 全局作用域中,用 const 和 l ...

  8. js中的extend的用法及其JS中substring与substr的区别

    1.    JS中substring与substr的区别 之前在项目中用到substring方法,因为C#中也有字符串的截取方法Substring方法,当时也没有多想就误以为这两种方法的使用时一样的. ...

  9. js中几个对象的区别和用法

    js中几个对象的区别和用法 今天总结一下js中几个对象的区别和用法: 首先来说说 parent.window与top.window的用法 "window.location.href" ...

最新文章

  1. c语言实现4x4随机字母,求用C++编写一个4x4矩阵运算类(元素float),实现转置和相乘,具体见补充,在线等!谢谢啦~...
  2. NTU 课程笔记:CV6422 样本分布
  3. java incompatible types: int cannot be converted to java.lang.Long
  4. Mybatis开发CRUD
  5. 《大数据》专题征文:医学大数据(截稿时间:2020年5月31日)
  6. 泉州中考分数如何计算机,2019年泉州中考总分多少分,泉州中考考试科目设置
  7. iQOO高层专访:打造性能旗舰 用户满意度为先
  8. LMbench - Tools for Performance Analysis | 内核基准测试
  9. 时间复杂度o(n^0.5)_算法基础1.1:算法复杂度计算(二)
  10. [转载] 利用python对csv文件进行简单的数据分析
  11. Android之HandlerThread
  12. SQLPrompt10带注册机(支持SSMS v18.5)
  13. coreseek java_coreseek
  14. EEGLAB教程-1.2通道定位
  15. mysql schemata_SCHEMATA · xiaoboluo768/mysql-system-schema Wiki · GitHub
  16. RC / RL并联电路计算
  17. No matter what,just do not give up。
  18. 18年研赛数学建模心得
  19. 一根木根随机折成三截能拼成三角形的概率
  20. 设计模式之禅【代理模式】

热门文章

  1. tcp欢动窗口机制_TCP协议中的窗口机制------滑动窗口详解
  2. redis源码分析 ppt_【Redis】redis各类型数据结构和底层实现源码分析
  3. cadence设计运算放大器_21.比较器的原理与特性,它与运算放大器的本质区别总结归纳...
  4. jvm内存结构_JVM系列之内存结构
  5. mongodb创建local库用户_mongodb用户与角色使用
  6. C语言数据类型转换详解
  7. Emulator: PANIC: Cannot find AVD system path. Please define ANDROID_SDK_ROOT
  8. 德州计算机速成班培训,德州办公软件培训速成班
  9. python基础(12)之匿名函数lambda
  10. iOS开发里面的4个层次