Multiple identities of JavaScript function objects

Source: Internet
Author: User

CopyCodeThe Code is as follows: function flower ()
{
This. Name = "Rose ";
This. color = "red ";
}
// Flower () as the constructor
VaR OBJ = new flower ();
// Output True, flower as class reference
Alert (OBJ instanceof flower );

The function keyword can declare a common function, which is the same as the function concept in other languages. In addition, it can be used for class declaration and implementation, object constructor, and class reference.
In the preceding example, the flower class is declared using the function keyword, and two attributes, name and color, are declared using the this keyword. Then, when you create an OBJ object, flower plays the role of object constructor again. Finally, the instanceof keyword is used to determine whether the OBJ object is an instance of the flower class. At this time, flower plays the role of class reference.

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.