Classes in JavaScript are actually implemented through functions. When we create an object using a literal or constructor, we only assign attributes and values to a specific object. If we have multiple objects whose attributes are the same but their values are different, we will write a lot of repeated statements. At this time, the class will be very useful.
Create a class for the constructor method as follows:
The Code is as follows:
Function className (prop_1, prop_2, prop_3 ){
This. prop1 = prop_1;
This. prop2 = prop_2;
This. prop3 = prop_3 ;}
With the above class, we can create an instance for the class:
The Code is as follows:
Var obj_1 = new className (v1, v2, v3)
Var obj_2 = new className (v1, v2, v3)
We can also add a method to the class, which is actually the Function in the Function.
The Code is as follows:
Function className (prop_1, prop_2, prop_3 ){
This. prop1 = prop_1;
This. prop2 = prop_2;
This. prop3 = prop_3;
This. func = function new_meth (property ){
// Coding here
}
}
Attribute access domain:
In JavaScript, the attributes of an object are global by default, that is, the object can be directly accessed inside and outside the object. In the above example, this. prop1, this. prop2, And this. prop3 are all global attributes.
How to define private attributes? Using var, in the following example, price becomes a private property!
The Code is as follows:
Function Car (listedPrice, color ){
Var price = listedPrice;
This. color = color;
This. honk = function (){
Console. log ("BEEP !! ");
};
}
If you want to access a private property, you can add a method to the object to return this private property. Because the method is in the object, you can access the private property of the object. You can access this private property by calling this method externally. However, this cannot be used in the method. As in the above example, to access the price, you can add the method to the object:
The Code is as follows:
This. getPrice = function (){
// Return price here!
Return price;
--------------------------------------------------------------------------------
Inheritance:
Use the following syntax to inherit:
The Code is as follows:
ElectricCar. prototype = new Car ();
Use instanceOf to check whether an object inherits. true or false is returned.
The Code is as follows:
MyElectricCar instanceof Car
Add Method to the inherited object:
The Code is as follows:
// Use the constructor to define a new object
Function ElectricCar (listedPrice ){
This. atomicity = 100;
Var price = listedPrice;
}
// Make the new object inherit the Car
ElectricCar. prototype = new Car ();
// Add a method for the new object
ElectricCar. prototype. refuel = function (numHours ){
This. atomicity = + 5 * numHours;
};
Override the value or method of the prototype object:
After we inherit the prototype object, we inherit the prototype value and method. However, sometimes our object values or methods may be different. At this time, we can rewrite the values and methods of the prototype object to change the content of the new object.
The Code is as follows:
Function Car (listedPrice ){
Var price = listedPrice;
This. speed = 0;
This. numWheels = 4;
This. getPrice = function (){
Return price;
};
}
Car. prototype. accelerate = function (){
This. speed + = 10;
};
Function ElectricCar (listedPrice ){
Var price = listedPrice;
This. atomicity = 100;
}
ElectricCar. prototype = new Car ();
// Override the accelerate Method
ElectricCar. prototype. accelerate = function (){
This. speed + = 20;
};
// Add the new method deceleratepoliciccar. prototype. decelerate = function (secondsStepped ){
This. speed-= 5 * secondsStepped;
};
MyElectricCar = new ElectricCar (500 );
Mypoliciccar. accelerate ();
Console. log ("myElectricCar has speed" + myElectricCar. speed );
MyElectricCar. decelerate (3 );
Console. log ("myElectricCar has speed" + myElectricCar. speed );