Javascript Judge object's specific class reprint _javascript technique

Source: Internet
Author: User
We all know that Javascript typeof can get the type of a variable, but the TypeOf return value is only six "number," "String," "Boolean," "Object," "function," and "undefined."

In fact, Javascript also has a number of special categories such as Array, Date. Why is it not possible to return in typeof?
The original Javascript was to classify the Array Date object into the object class. We can only judge the exact category of object by instanceof.

Here is a simple example to illustrate the use of instanceof
Copy Code code as follows:

function Objtest (obj) {
var i, T, s = ""; Create a variable.
t = new Array (); Creates an array.
t["date" = date; Fills the array.
T["Object" = object;
t["Array" = array;
for (i in T)
{
if (obj instanceof t[i])//Check the class of obj.
{
s + = "obj is an instance of" + i + "\ n";
}
Else
{
s + = "obj is isn't an instance of" + i + "\ n";
}
}
return (s); Returns a string.
}

var obj = new Date ();
Response.Write (Objtest (obj));

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.