javascript代码实例教程-判断js中的类型:typeof / instanceof / constructor / prototype

发布时间:2019-01-27 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了javascript代码实例教程-判断js中的类型:typeof / instanceof / constructor / prototype脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 如何判断js中的类型呢,先举几个例子:

VAR a = "jason";

var b = 123;

var c = true;

var d = [1,2,3];

var e = new Date();

var f = function(){
alert('jason');

};


一、最常见的判断方法:tyPEof

typeof是一个一元运算符,它返回的结果始终是一个字符串,对不同的操作数,它返回不同的结果,另外typeof可以判断function的类型;在判断除Object类型的对象时比较方便。

console.LOG(typeof a == "string"); //true

console.log(typeof a == String); //false
具体的规则如下:

1) 对于数字类型的操作数而言, typeof 返回的值是 number。比如说:typeof 1,返回的值就是number。

上面是举的常规数字,对于非常规的数字类型而言,其结果返回的也是number。比如typeof NaN,NaN在JavaScript中代表的是特殊非数字值,虽然它本身是一个数字类型。

在JavaScript中,特殊的数字类型还有几种

InfinITy //表示无穷大特殊值

NaN //特殊的非数字值

Number.MAX_VALUE //可表示的最大数

Number.MIN_VALUE //可表示的最小数字(与零最接近)

Number.NaN //特殊的非数字值

Number.POSITIVE_INFINITY //表示正无穷大的特殊值

Number.NEGATIVE_INFINITY //表示负无穷大的特殊值

以上特殊类型,在用typeof进行运算进,其结果都将是number。


2) 对于字符串类型,typeof返回的值是string。比如typeof "jason"返回的值是string。

3) 对于布尔类型,typeof返回的值是boolean。比如typeof true返回的值是boolean。

4) 对于对象、数组、null返回的值是object。比如typeof {},typeof [],typeof null返回的值都是object。


5) 对于函数类型,返回的值是function。比如:typeof eval,typeof Date返回的值都是function。

6) 如果运算数是没有定义的(比如说不存在的变量、函数或者undefined),将返回undefined。比如:typeof jason、typeof undefined都返回undefined。

console.log(typeof a); //string

console.log(typeof b); //number

console.log(typeof c); //boolean

console.log(typeof d); //object

console.log(typeof e); //object

console.log(typeof f); //function

console.log(typeof 1); //number

console.log(typeof NaN); //number

console.log(typeof Number.MIN_VALUE); //number

console.log(typeof Infinity); //number

console.log(typeof "123"); //string

console.log(typeof true); //boolean

console.log(typeof {}); //object

console.log(typeof []); //object

console.log(typeof null); //object

console.log(typeof eval); //function

console.log(typeof Date); //function

console.log(typeof sss); //undefined

console.log(typeof undefined); //undefined


二、判断一个对象是否为某一数据类型,或一个变量是否为一个对象的实例:instanceof

注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。

console.log(d instanceof Array); //true

console.log(e instanceof Date); //true

console.log(f instanceof Function); //true


三、根据对象的constructor判断:constructor

console.log(d.constructor === Array) //true

console.log(e.constructor === Date) //true

console.log(f.constructor === Function) //true

注意constructor在类继承时会出错

例如:

function A(){};

function B(){};

var aObj = new A();

console.log(aObj.constructor === A); //true;

console.log(aObj.constructor === B); //false;

function C(){};

function D(){};

C.PRototype = new D(); //C继承自D

var cObj = new C();

console.log(cObj.constructor === C); //false;

console.log(cObj.constructor === D); //true;

而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:

console.log(cObj instanceof C); //true

console.log(cObj instanceof D); //true

解决construtor的问题通常是让对象的constructor手动指向自己:

cObj.constructor = C; //将自己的类赋值给对象的constructor属性

console.log(cObj.constructor === C); //true;

console.log(cObj.constructor === D); //false; 基类不会报true了;


四、通用但很繁琐的方法:prototype

console.log(Object.prototype.toString.call(a) === '[object String]'); //true

console.log(Object.prototype.toString.call(b) === '[object Number]'); //true

console.log(Object.prototype.toString.call(c) === '[object Boolean]'); //true

console.log(Object.prototype.toString.call(d) === '[object Array]'); //true

console.log(Object.prototype.toString.call(e) === '[object Date]'); //true

console.log(Object.prototype.toString.call(f) === '[object Function]'); //true

注:大小写不能写错,比较麻烦,但胜在通用。


总结:

通常情况下用typeof判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,简单总结下,欢迎补充!


觉得可用,就经常来吧! 脚本宝典 欢迎评论哦! js脚本,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的javascript代码实例教程-判断js中的类型:typeof / instanceof / constructor / prototype全部内容,希望文章能够帮你解决javascript代码实例教程-判断js中的类型:typeof / instanceof / constructor / prototype所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。