Javascript determines whether a specific class of an object is reprinted

Source: Internet
Author: User

We all know that Javascript typeof can obtain the type of the variable, but typeof has only six return values: "number," "string," "boolean," "object," "function, "and" undefined."

In fact, Javascript still has many special categories, such as Array and Date. Why can't they be returned in typeof?
Originally, Javascript classifies Array Date objects into Object classes. We can only use instanceof to determine the exact object category.

Here is a simple example to illustrate the usage of instanceof.
Copy codeThe Code is as follows:
Function objTest (obj ){
Var I, t, s = ""; // create a variable.
T = new Array (); // create an Array.
T ["Date"] = Date; // fill in the array.
T ["Object"] = Object;
T ["Array"] = Array;
For (I in t)
{
If (obj instanceof t [I]) // check the obj class.
{
S + = "obj is an instance of" + I + "\ n ";
}
Else
{
S + = "obj is not an instance of" + I + "\ n ";
}
}
Return (s); // return a string.
}

Var obj = new Date ();
Response. write (objTest (obj ));

Related Article

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.