How to define classes in javascript _ javascript skills

Source: Internet
Author: User
This article mainly introduces in detail information about the methods for defining classes in javascript. You can refer to the following methods for defining classes in javascript:

1. Factory Mode

The Code is as follows:


Function Car (){
Var ocar = new Object;
Ocar. color = "blue ";
Ocar. doors = 4;
Ocar. showColor = function (){
Document. write (this. color)
};
Return ocar;
}
Var car1 = Car ();
Var car2 = Car ();

When this function is called, a new object is created and all attributes and methods are assigned to it. You can use this function to create two objects with identical attributes. Of course, my sister can change this method by passing parameters to it.

The Code is as follows:


Function Car (color, door ){
Var ocar = new Object;
Ocar. color = color;
Ocar. doors = door;
Ocar. showColor = function (){
Document. write (this. color)
};
Return ocar;
}
Var car1 = Car ("red", 4 );
Var car2 = Car ("blue", 4 );
Car1.showColor () // output: "red"
Car2.showColor () // output: "blue"

Now you can pass different parameters to the function to get objects with different values.
In the previous example, showcolor () is created every time the function Car () is called, which means that each object has its own showcolor () method.

But in fact, each object bucket shares the same function.
Although you can define a method outside a function, and then point the function property to this method.

The Code is as follows:


Function showColor (){
Alert (this. color );
}
Function Car (){
Var ocar = new Object ();
Ocar. color = color;
Ocar. doors = door;
Ocar. showColor = showColor;
Return ocar;
}

However, this does not seem like a function method.

2. constructor Method

The constructor method is as simple as the factory method, as shown below:

The Code is as follows:


Function Car (color, door ){
This. color = color;
This. doors = door;
This. showColor = function (){
Alert (this. color)
};
}
Var car1 = new Car ("red", 4 );
Var car2 = new Car ("blue", 4 );

You can see that the constructor method does not create an object inside the function. this keyword is used. Because the object has been created when the constructor is called, this can only be used within the function to access object attributes.
Now we use new to create an object. It looks like this is the case! But it is the same as the factory method. Each call creates its own method for the object.

3. Prototype

This method utilizes the prototype attribute of the object. First, use an empty function to create a class name. Then, all attributes and methods are assigned the prototype attribute.

The Code is as follows:


Function Car (){
}
Car. prototype. color = "red ";
Car. prototype. doors = 4;
Car. prototype. showColor = function (){
Alert (this. color );
}
Var car1 = new Car ();
Var car2 = new Car ();

In this Code, an empty function is defined first, and then the properties of the object are defined through the prototype attribute. When this function is called, all attributes of the prototype are immediately assigned to the object to be created. All objects of this function are stored as pointers to showColor, the syntax seems to belong to the same object.
However, this function does not have a parameter and cannot be initialized by passing a parameter. You must create an object before changing the default value of the property.
A serious problem with the prototype method is that when the attribute points to an object, such as an array.

The Code is as follows:


Function Car (){
}
Car. prototype. color = "red ";
Car. prototype. doors = 4;
Car. prototype. arr = new Array ("a", "B ");
Car. prototype. showColor = function (){
Alert (this. color );
}
Var car1 = new Car ();
Var car2 = new Car ();
Car1.arr. push ("cc ");
Alert (car1.arr); // output: aa, bb, cc
Alert (car2.arr); // output: aa, bb, cc

Here, because of the reference value of the array, the two objects of Car point to the same array, so when the value is added in car1, it can be seen in car2.
Consortium allows you to create objects in the same way as other programming languages by using constructors or prototypes. Consortium defines non-functional attributes of objects by using constructors and defines object methods by prototype.

The Code is as follows:


Function Car (color, door ){
This. color = color;
This. doors = door;
This. arr = new Array ("aa", "bb ");
}
Car. prototype. showColor (){
Alert (this. color );
}
Var car1 = new Car ("red", 4 );
Var car2 = new Car ("blue", 4 );
Car1.arr. push ("cc ");
Alert (car1.arr); // output: aa, bb, cc
Alert (car2.arr); // output: aa, bb


5. Dynamic Prototype

The dynamic prototype method is similar to the hybrid constructor/prototype method. The only difference is to assign the location of the object method.

The Code is as follows:


Function Car (color, door ){
This. color = color;
This. doors = door;
This. arr = new Array ("aa", "bb ");
If (typeof Car. _ initialized = "undefined "){
Car. prototype. showColor = function (){
Alert (this. color );
};
Car. _ initialized = true;
}
}

The dynamic prototype uses a flag to determine whether a method has been assigned to the prototype. This ensures that this method is created only once.

6. Hybrid Factory

Its target teacher creates a false constructor and returns only new instances of another object.

The Code is as follows:


Function Car (){
Var ocar = new Object ();
Ocar. color = "red ";
Ocar. doors = 4;
Ocar. showColor = function (){
Alert (this. color)
};
Return ocar;
}

Unlike the factory method, this method uses the new operator.

The above are all object creation methods. Currently, hybrid Constructors/prototypes are the most widely used methods. In addition, dynamic prototypes are also popular. The function is equivalent to the constructor/prototype.

The above is all the content of this article. I hope you will like it.

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.