typeof和instanceof简介及用法

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")。

  

时间: 2024-07-28 13:47:34

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

js中typeof与instanceof的不同用法

typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果: number,boolean,string,function(函数),object(NULL,数组,对象),undefined. 如: alert(typeof (123));//typeof(123)返回"number" alert(typeof ("123"));//typeof("123")返回"string" 我们可以使用typeof

typeof()和instanceof()用法区别

typeof()和instanceof()用法区别: 两者都是用来判断数据类型的 typeof()是能用来判断是不是属于五大类型Boolean,Number,String,Null,Undefined的,是比较宏观的判断: instanceof()判断数据类型相对typeof()来说更深入,能判断更具体的,比如Array,object,Boolean,Number,Strin等.

js中typeof和instanceof用法区别

typeof和instanceof的区别 typeof和instanceof都可以用来判断变量,它们的用法有很大区别: typeof会返回一个变量的基本类型,只有以下几种:number,boolean,string,object,undefined,function:例: alert(typeof(1));//number alert(typeof("abc"));//string alert(typeof(true));//boolean alert(typeof(m));//und

JavaScript中instanceof运算符的用法以及和typeof的区别

instanceof : 为判断一个对象是否为某一数据类型,或一个变量是否为一个对象的实例;返回boolean类型栗子①: <script type="text/javascript"> var aColors = ["red", "green", "blue"]; alert(typeof aColors[0]); //output "string" alert(aColors[0] inst

JS中 typeof 与 instanceof 的区别

JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的.但它们之间还是有区别的:typeof是一个一元运算,放在一个运算数之前,运算数可以是任意类型.它返回值是一个字符串,该字符串说明运算数的类型.typeof 一般只能返回如下几个结果:number,boolean,string,function,object,undefined.(Array,Null 均返回 object ,无法判断这些类型)例子: typeof(1): number

js typeof 与 instanceof的区别

js中typeof与instanceof用法小记 今天写JS代码,遇到动态生成多个名称相同的input复选按钮 需要判断其是否是数组,用到了if (typeof(document.MapCheckMgr.checkid)!="undefined") 以前用得少,就顺便查了一下关于typeof的那些事 typeof用以获取一个变量或者表达式的类型,typeof一般只能返回如下几个结果: number,boolean,string,function(函数),object(NULL,数组,对

typeof 与 instanceof 运算符

相信很多伙伴们在刚刚入门js的时候,都会遇到 typeof 与 instanceof 在用法上的一些个困惑,今天小编就要和大家聊一聊它们各自的用法以及一些区别, 您先别急,咱下面就来分别讲解: js是一门弱语言,它在声明变量时无需确定变量的类型,js在运行时会自动判断. typeof  用来检测一个变量的类型,  返回值是一个字符串. 使用方式:typeof(表达式)和 typeof 变量名 运算符返回值:字符串,有七种可能:"undefined" ,"number"

比较typeof与instanceof

相同点: JavaScript中typeof和instanceof常用来判断一个变量是否为空,或者是什么类型的. 不同点: typeof的定义和用法: 返回值是一个字符串,用来说明变量的数据类型. 细节: 1).typeof一般只能返回如下几个结果: number,boolean,string,function,object,undefined. 2).typeof来获取一个变量是否存在,如if(typeof a!="undefined"){alert("ok")}

typeof与instanceof

最近买了部安卓的手机,google nexus5 系统是安卓4.4.2. 刚到手就发现链接wifi有问题,一直在获取ip(obtaining ip...)和验证.试过恢复出厂 重启 各种都不管用,只有设置静态ip才可以,但是不能一直这样子呀!! 查了下路由器,路由器已经分配了地址.所以最大可能就是安卓手机上拿到这个地址没有成功写入配置文件,为什么没有写入呢,就是权限的问题了,不明白为什么google会出现这个错误. 因为不熟悉安卓系统,所以查了好几天,终于在一个外国网站上发现了下面这个解决办法,