js中判断数据类型的属性typeof、instanceof、 constructor、 prototype


javascript中,有5中原始类型

5种原始类型分别为:undefined、null、boolean、number、string。

除了原始类型,还有引用类型。


引用类型,也就对象类型

如:var o=newObject();

    vard=new Date; //js中new对象的时候,括号是可以省略的

    //以上的o、d,就是引用类型



       

        var a = "程陈";//string
        var b = 1993;//number
        var c = [1,2,3];//Array
        var d = new Date();//Date
        var e = function(){alert("123")};//function
        var f = function(){this.name="123";};//function
        var g //未定义
        var h = true;//boolean


  • typeof运算符

  • 最常见的判断方法:typeof

        //使用typeof(类型)
        document.write("typeof判断string的输出--->"+typeof a +"<br>");
        document.write("typeof判断number的输出--->"+typeof b+"<br>");
        document.write("typeof判断数组的输出--->"+typeof c+"<br>");
        document.write("typeof判断Date的输出--->"+typeof d+"<br>");
        //typeof不能判断object具体是什么(array、data。。。)
        document.write("typeof判断函数的输出--->"+typeof e+"<br>");
        document.write("typeof判断函数的输出--->"+typeof f+"<br>");
        document.write("typeof判断未定义的输出--->"+typeof g+"<br>");
        document.write("typeof判断boolean的输出--->"+typeof h+"<br>");
        document.write("typeof判断null的输出--->"+typeof i+"<br>");


注:typeof null //得到"object",typeof运算符对于null值会返回"object"。这实际上是JavaScript最初实现中的一个错误,然后被ECMAScript沿用了

其中typeof返回的类型都是字符串形式,需注意,例如:

        console.log("typeof---正确的判断\"string\"--"+ (typeof a=="string"));
        console.log("typeof---正确的判断String--"+(typeof b == String ));

另外typeof可以判断function的类型;在判断除Object类型的对象时比较方便。



instanceof运算符,判断已知对象类型的方法

在引用类型值判断类型的时候,typeof运算符会出现一个问题,无论引用的是什么类型的对象,它都返回"object"。

ECMAScript引入了另一个Java运算符instanceof来解决这个问题。

示例:

function Person(){} 

function User(){} 

var u=new User;

console.log( u instanceof Person ); //false

console.log( u instanceof User ); //true

例子解析:

“变量u是否为Person对象的实例?得到false。

“变量u是否为User对象的实例?得到true。

因为typeof在判断对象类型的值的时候,有很大的局限性,

所以instanceof会是一个不错的选择,instanceof才能判断一个值具体是由什么构造函数构造出来的。

        console.log("instanceof---正确的判断集合--"+ (c instanceof Array));//判断的类型是区分大小写的
        console.log("instanceof---正确的判断Function--"+ (e instanceof Function));
        // console.log("instanceof---正确的判断function--"+ (e instanceof function));//不能这样写function 哦
        console.log("instanceof---正确的判断Date--"+ (d instanceof Date));


Object.constructor属性

javascript中的所有对象都继承自Object。

constructor是Object的一个属性,他指向:创建对象的函数的引用(指针)。(可以理解为constructor指向对象的构造函数)

简单示例:

         functionUser(){} 

var u=new User;

console.log(u.constructor===User );//得到true,也就是说对象的constructor属性指向他的构造函数。

console.log(u.constructor.name );//得到User,也就是构造函数的名称

注:constructor属性并非一定指向构造函数,他也是可以修改、变更的。

alert(c.constructor ===Array) ----------> true

alert(d.constructor === Date)-----------> true

alert(e.constructor ===Function) -------> true

注意: constructor 在类继承时会出错

eg,

     function A(){};

     function B(){};

     A.prototype = new B(); //A继承自B

     var aObj = new A();

     alert(aobj.constructor === B) ----------->true;

     alert(aobj.constructor === A) ----------->false;

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

     alert(aobj instanceof B) ---------------->true;

     alert(aobj instanceof B) ---------------->true;

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

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

     alert(aobj.constructor === A) ----------->true;

     alert(aobj.constructor === B) ----------->false; //基类不会报true了;



通用但很繁琐的方法: prototype

alert(Object.prototype.toString.call(a) === ‘[object String]’)-------> true;

alert(Object.prototype.toString.call(b) === ‘[object Number]’)-------> true;

alert(Object.prototype.toString.call(c) === ‘[object Array]’)-------> true;

alert(Object.prototype.toString.call(d) === ‘[object Date]’)-------> true;

alert(Object.prototype.toString.call(e) === ‘[object Function]’)-------> true;

alert(Object.prototype.toString.call(f) === ‘[object Function]’)-------> true;

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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值