自己不官方的理解typeof和instanceof
2017-02-16 16:32
357 查看
一、typeof
typeof(的)运算数未定义,返回(的)就是 "undefined".
运算数为数字 typeof(x) = "number"
字符串 typeof(x) = "string"
布尔值 typeof(x) = "boolean"
对象,数组和null typeof(x) = "object"
函数 typeof(x) = "function"
综上所述,typeof的返回类型有:undefined,number,string,boolean,object,function
自己理解的用途:
我们可以使用 typeof 来获取一个变量是否存在,如 if(typeof a!="undefined"){alert("ok")},而不要去使用 if(a) 因为如果 a 不存在(未 声明)则会出错。
二、instanceof
instanceof运算符是返回一个 Boolean 值,指出对象是否是特定类的一个实例.
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) 会返回true.
typeof(的)运算数未定义,返回(的)就是 "undefined".
运算数为数字 typeof(x) = "number"
字符串 typeof(x) = "string"
布尔值 typeof(x) = "boolean"
对象,数组和null typeof(x) = "object"
函数 typeof(x) = "function"
综上所述,typeof的返回类型有:undefined,number,string,boolean,object,function
自己理解的用途:
我们可以使用 typeof 来获取一个变量是否存在,如 if(typeof a!="undefined"){alert("ok")},而不要去使用 if(a) 因为如果 a 不存在(未 声明)则会出错。
二、instanceof
instanceof运算符是返回一个 Boolean 值,指出对象是否是特定类的一个实例.
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) 会返回true.
相关文章推荐
- JavaScript instanceof理解及与typeof对比
- 深入理解javascript之typeof和instanceof
- js基础2-深入理解javascript之typeof和instanceof
- js typeof 与 instanceof的区别
- instanceof和typeof运算符的区别详解
- typeof和instanceof简介及用法
- 自己对IoC(Inversion of Control)的理解
- JS typeof和instanceof用法
- JS中的instanceof和typeof
- typeof和instanceof
- typeof与instanceof的区别
- js的instanceof 运算符vs typeof 运算符
- javascript instanceof运算符 以及和 typeof的区别
- 如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较
- typeof和instanceof
- javascript之小积累-.-typeof与instanceof的区别
- JS中的instanceof和typeof
- typeof和instanceof检测数据类型
- JS typeof与instanceof的区别
- boost在自己主动确定数据类型(BOOST_TYPEOF和BOOST_AUTO)使用