JavaScript Judging Object types

Source: Internet
Author: User

The following references http://bushihentian.blog.163.com/blog/static/187174306201401371757775/

Way One

The return value using the TypeOf keyword has ' string ' number ' Boolean ' undfined ' function ' ' object ' total six minutes
typeof//typeof can be used to detect the data type of a given variable, returning a string
Console.log (typeof "); String
Console.log (typeof null); Object
Console.log (typeof 1); Number
Console.log (typeof {name: ' name '}); Object
Console.log (typeof undefined); Undefined
Console.log (typeof [up]); Object
Console.log (typeof true); Boolean
Console.log (typeof function () {}); function
Summary: From the above example, it can be seen that except for null, the object, the array of other variable types can accurately detect what the data type.
Null, object, array all returned object

Determine what the base type is using typeof, and if it is ' object ', you can use ToString to get what type of object it is.

Way Two

Using the Instanseof keyword

Instanseof//used to determine whether a variable is an instance of an object, returns a Boolean value

var str = ' 123 ';
Console.log (str instanceof String); False

var num = 123;
Console.log (num instanceof number); False

var bool = true;
Console.log (bool instanceof Boolean); False

var arr = [n/a];
Console.log (arr instanceof Array); True

var obj = {name: ' name '};
Console.log (obj instanceof Object); True

Summary: As can be seen from the above example, the normal defined variables return true except for arrays and objects, and all other simple types return false.

Mode three

Using the constructor property

constructor//Each variable has a constructor property that points to its own constructor

var str = ' 123 ';
Console.log (Str.constructor = = = String); True

var num = 123;
Console.log (Num.constructor = = number); True

var bool = true;
Console.log (Bool.constructor = = = Boolean); True

var arr = [n/a];
Console.log (Arr.constructor = = = Array); True

var obj = {name: ' name '};
Console.log (Obj.constructor = = = Object); True

Summary: As you can see from the example above, the constructors of each variable point to their type.

Mode Four

The prototype of all objects ultimately point to the prototype property of object, so the ToString method of object is used to get the return value.

var str = ' 123 ';
Console.log (Object.prototype.toString.call (str)); "[Object String]"

var num = 123;
Console.log (Object.prototype.toString.call (num)); "[Object number]"

var bool = true;
Console.log (Object.prototype.toString.call (bool)); "[Object Boolean]"

var arr = [n/a];
Console.log (Object.prototype.toString.call (arr)); "[Object Array]"

var obj = {name: ' name '};
Console.log (Object.prototype.toString.call (obj)); "[Object Object]"


var obj = null;

Object.prototype.toString.call (str); "[Object Null]"

Summary: Use regular or character truncation to get the type string beginning with uppercase, and then get the data type of the corresponding variable by using the String.tolowercase () method.

Only to find these methods for the time being, we can use the corresponding way to encapsulate the GetType () method according to our own needs.

Notice that the custom object returns "[Object Object]" as:

function person () {}
var p=new person ();

(Object.prototype.toString.call (P);//"[Object Object]"

JavaScript determines object type

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.