类型识别的几种方法

  在js中识别数据类型有四种方法

  分别是:1. typeof  2. Object.prototype.toString  3. constructor  4. instanceof

  这几种方法各有优缺点

  先说一下js中的数据类型

  js中一共有十五种类型称之为原生对象

  Undefined、Null、Number、String、Boolean、Object、Function、Array、Date、RegExp、Error、Math、JSON、全局对象、arguments

  其中:

标准类型:Undefined、Null、Number、String、Boolean、Object

构造器:Object、Number、String、Boolean、Function、Arra、Date、RegExp、Error

对象:Math、JSON、全局对象、arguments

标准内置对象:Number、String、Boolean、Object、Function、Array、Date、RegExp、Error、Math、JSON、全局对象

  一 typeof:能识标准类型(Null除外),能识别引用类型(Function除外)

 1     alert(typeof 'abc');    //string
 2     alert(typeof 12);       //number
 3     alert(typeof true);     //boolean
 4     alert(typeof undefined);    //undefined
 5     alert(typeof null);         //object
 6     alert(typeof { name: 1});   //object
 7     alert(typeof function(){}); //function
 8     alert(typeof []);           //object
 9     alert(typeof new Date);     //object
10     alert(typeof /\d/);         //object
11     function Parent(){}        //自定义对象
12     alert(typeof new Parent);   //object

  二 Object.prototype.toString:能识别标准类型及内置对象类型但不能识别自定义类型

   function type(obj){      //封装Object.prototype.toString
       return Object.prototype.toString.call(obj).slice(8,-1).toLowerCase();
   }
   alert(type('abc'));      //string
   alert(type(123));        //number
   alert(type(true));       //boolean
   alert(type(undefined));  //undefined
   alert(type(null));       //null
   alert(type({name:1}));   //object
   alert(type(function(){}));   //function
   alert(type([]));             //array
   alert(type(new Date));       //date
   alert(type(/\d/));           //regexp
   function Parent(){}
   alert(type(new Parent));     //object

   三 constructor:识别标准类型(Undefined\Null除外)、识别内置对象类型、识别自定义对象类型

//    判断原始类型
   console.log('abc'.constructor === String);   //true
   console.log((123).constructor === Number);   //true
   console.log(true.constructor === Boolean);   //true
   console.log({name:1}.constructor === Object);//true
//    判断内置对象类型
   console.log([].constructor === Array);       //true
//    判断自定义对象(构造函数)
   function Parent(name){
       this.name = name;
   }
   console.log(new Parent('二珂').constructor === Parent);  //true

  四 instanceof:判别内置对象类型、不能判别原始类型、判别自定义对象类型和父子类型

// 能够判别内置对象类型
   console.log([] instanceof Array);        //true
   console.log(/\d/ instanceof RegExp);     //true
// 不能判别原始对象类型
   console.log((123) instanceof Number);    //false
   console.log('爱你呦' instanceof String); //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);       //将Point的作用域借给Circle使用
    this.radius = r;
}
Circle.prototype = new Point();     //Circle是Point的子对象
Circle.prototype.constructor = Circle;
var c = new Circle(1, 1, 2);
console.log(c instanceof Circle);       //true
console.log(c instanceof Point);        //true

 五 jQuery中的 $.type()方法;

 

posted @ 2018-05-29 10:35  Moriart谭  阅读(473)  评论(0编辑  收藏  举报