码迷,mamicode.com
首页 > Web开发 > 详细

js中类型识别的方法

时间:2016-07-21 00:36:17      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

第一种方法typeof

typeof是一种运算符,它的值有以下几种

技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ces</title>
</head>
<body>
    <script>
    console.log(typeof "Liesbeth");//"string"
    console.log(typeof 12);//"number"
    console.log(typeof true); //"boolean"
    console.log(typeof undefined);//"undefined"
    console.log(typeof null);//"object"
    console.log(typeof {name:"Liesbeth"});//"object"
    console.log(typeof function(){});//"function"
    console.log(typeof []);//"object"
    console.log(typeof new Date);//"object"
    console.log(typeof /[0-9]/);//‘object‘
    console.log(typeof new Number(123));//‘object‘
    function Person(){};
    console.log(typeof new Person);//‘object‘

    </script>

</body>
</html>
typeof

 

运行结果如下,可以看出typeof能判断基本类型的值,其他通过new创建的类型除了function外,其他都是object,其中null也是object

技术分享

第二种方法instanceof

instanceof运算符可以用来判断某个构造函数的prototype属性所指向的對象是否存在于另外一个要检测对象的原型链上即某个对象是否是某个构造函数的实例,如下

技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ces</title>
</head>
<body>
    <script>
    //能判别内置对象内型
    console.log([] instanceof Array);//true
    console.log([] instanceof Object);//true    
    console.log(/[0-9]/ instanceof RegExp);//true
    console.log(new String(123) instanceof String);//true
    console.log(function(){} instanceof Function);//true

    //不能判断原始类型
    console.log(123 instanceof String);//false
    console.log(123 instanceof Number);//false
    console.log(true instanceof Boolean);//false

   //判别自定义对象类型
    function Point(x,y){
        this.x=x;
        this.y=y;
    }
    function Circle(x,y,r){
        Point.call(this,x,y);
        this.radius=r;
    }
    Circle.prototype=new Point();
    Circle.prototype.constructor=Circle;
    var c= new Circle(1,1,2);
    console.log(c instanceof Circle);//true
    console.log(c instanceof Point);//true
    </script>

</body>
</html>
instanceof

运行结果如下图

技术分享

 

由此可看出instanceof不能识别原始类型,能识别自定义对象和内置对象类型同时它们都属于Object

 第三种方法Object.prototype.toString.call

这个方法获取的是对象的[[Class]]属性值,返回结果是固定的‘[object‘+[[class]]属性+‘]‘,jquery中的$.type()就是利用这个属性判断对象类型的。

 

function type(obj){
  return Object.prototype.toString.call(obj).slice(8,-1);
}

 

运行结果如下:

技术分享

其中的type(c)为instanceof时创建的对象。

可以看出toString能识别原始类型和内置对象,但是不能识别自定义对象类型,另外在ie7和8中null和undefined都是返回"Object"如下

技术分享

同时在IE6中字符串也会返回"Object"所以用的时候要多注意这几个值。

第四种方法constructor

constructor 属性返回对创建此对象的数组函数的引用,如图所示

技术分享

利用此特性我们可以判别类型,其中由于null和undefined没有构造函数所以特殊处理,编写函数如下

function getConstructorName(obj){
    return (obj===undefined||obj===null)?obj:(obj.constructor&&obj.constructor.toString().match(/function\s*([^(]*)/)[1]);
}

 运行结果如下

技术分享

可以看出constructor能判别原始类型(除null和undefined),内置对象和自定义对象.

参考资料:http://www.cnblogs.com/ziyunfei/archive/2012/11/05/2754156.html其中对Object.prototype.toString解释的清楚。

 

 

 

js中类型识别的方法

标签:

原文地址:http://www.cnblogs.com/liziyu91/p/5689960.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!