One of JavaScript learning: which method is used to define classes or objects?

Source: Internet
Author: User

There are multiple methods to define classes or objects in javascript:

Create a car as an instance.

VaR ocar = new object;
Ocar. color = "red ";
Ocar. Doors = 4;
Ocar. mpg = 23;
Ocar. showcolor = function ()
{
Alert (this. Color );
}

  • Factory construction, for example:

    Function createcar (scolor, idoors, impg)
    {
    VaR otempcar = new object;
    Otempcar. Color = scolor;
    Otempcar. Doors = idoors;
    Otempcar. mpg = impg;
    Otempcar. showcolor = function ()
    {
    Al (this. color)
    }
    Return otempcar;
    }

Disadvantage: each object has its own showcolor function.

  • Constructor method:

Function car (scolor, idoors, impg)
{
This. Color = scolor;
This. Doors = I;
This. mpg = impg;
This. showcolor = function ()
{
Alert (this. color)
};
}

Disadvantages: the constructor repeatedly generates a function. You can use an external function to rewrite the constructor (not quite familiar );

  • Prototype: The prototype attribute is used.

Function car ()
{
 
}
Car. Prototype. color = "red ";
Car. Prototype. Doors = 4;
Car. Prototype. mpg = 23;
Car. Prototype. Drivers = new array ("Mike", "Sue ");
Car. Prototype. showcolor = function ()
{
Alert (this. Color );
}

Disadvantage: add drivers to one instance and add another instance.

  • Hybrid constructor and prototype

Function car ()
{
 
}
Car. Prototype. color = "red ";
Car. Prototype. Doors = 4;
Car. Prototype. mpg = 23;
Car. Prototype. Drivers = new array ("Mike", "Sue ");
Car. Prototype. showcolor = function ()
{
Alert (this. Color );
}
Two drawbacks are solved.

  • Dynamic Prototype Construction

Function car (scolor, idoors, impg)
{
This. Color = scolor;
This. Doors = idoors;
This. mpg = impg;
This. Drivers = new array ("Mike", "Sue ");
 
If (typeof car. _ initialized = "undefined ")
{
Car. Prototype. showcolor = function ()
{
Alert (this. color)
}
Car. _ initialized = true;
}
}

  • Hybrid factory mode.

Function car ()
{
VaR otempcar = new object;
Otempcar. color = "red ";
Otempcar. Doors = 4;
Otempcar. mpg = 23;
Otempcar. showcolor = function ()
{
ALE (this. color)
}
Return otempcar;
}

Which method is used:

Currently, constructors and prototypes are popular, and dynamic prototypes are also popular.

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.