JS Object-Oriented Programming: How to Detect Object types

Source: Internet
Author: User

In JS, there are three main types of detection objects, typeof,instanceof,constructor, which can detect the type of objects, but there are some differences.



1 use typeof to detect object types.

Typeo, as the most commonly used method of detecting types, returns a string type, using the following:

function Testtype (value) {var str=typeof (value);//  alert (str);    Switch (str)   {case    ' undefined '://undefined type case      ' object '://null type, arbitrary built-in object, array case    ' Boolean '://TR Ue,false type Case    ' string '://String type case    ' function '://  arbitrary function case    ' number '://arbitrary numeric type, containing Nan  }  }

As you can see, the type can be tested for the most basic type, but for others, including dates, arrays, and so on, most of them return the object type, and null also returns the object type, which means there is no way to know the exact type.


Another improved detection method is to use the default ToString, which inherits from object and can return type information

function Classof (o) {      if (o = = = null) return "NULL";      if (o = = = undefined) return "undefined";      return Object.prototype.toString.call (o). Slice (8,-1);   } function Testtype (value) {var str=classof (value);//typeof (value);   alert (str);   Switch (str)   {case    ' undefined '://undefined type case      ' object '://Object Case    ' Boolean '://True,false type 
   case ' string '://String type case ' function '://  arbitrary function case ' number '://arbitrary numeric type, containing Nan case            ' date '://Date case ' Array '://Arrays case ' REGEXP '://Regular  }}

You can see that some improvements have been made, but there is still a large part of the object type.



2 using instanceof to detect object types

For typeof detection of type object, you can use instanceof to further detect the specific type. Instanceof actually detects the prototype of the object.

You can detect if a variable is an instance of an object and return a bool value.

For example:

                       var value=new date (), var isdate= value instanceof Date alert (isdate);

3 Detecting object types using the constructor detection object type

constructor is equivalent to detecting a constructor and returning a function

For example:

function TestConstructor (value) {   var str=value.constructor;   Switch (value.constructor)   {case   number://numeric type break    ;   }  alert (str);}


If you need to detect the exact type of an object, you can use these three methods synthetically

For example:

function Type (o) {    var t, c, N;  Type, class, name    //is a null type:    if (o = = = null) return "NULL";    is a special type in the numeric value: Nan:    if (o!== o) return "NaN";    Use typeof detection to remove other types of type "Object".       if ((t = typeof O)!== "Object") return T;    typeof detection is of type "object", then further detection    //can detect most built-in type    if ((c = classof (o))!== "Object") return C;    Classof (O) when returned as "Object", detects constructor    if (o.constructor && typeof o.constructor = = = "Function" & &        (n = o.constructor.getname ())) return n;    Unrecognized other type, default to "Object"    Return "Object";} function Classof (o) {    return Object.prototype.toString.call (o). Slice (8,-1);};    Returns the name of the function, possibly "" or nullFunction.prototype.getName = function () {    if ("name" in this) return this.name;    return this.name = This.tostring (). Match (/function\s* ([^ (]*) \ (/) [1];};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.