1. Factory mode
<script>
function Createcar () {
var ocar = new Object ();
Ocar.color= "Red";
ocar.doors=4;
Ocar.showcolor=function () {
alert (This.color);
}
return ocar;
}
Call
var oCar1 = Createcar ();
var oCar2 = Createcar ();
Ocar1.color = "BLACK";
Ocar1.showcolor ();
Ocar2.showcolor ();
</script>
Using the New keyword has been popular, so we use the method above to define the total feeling awkward,
It is also impractical to create new properties and functions on each invocation.
Let's look at the formal definition class of the constructor.
2, constructor
<script>
function Car (color,doors) {
This.color = color;
this.doors = doors;
This.showcolor = function () {
alert (this.color);
}
}
//Call
var car1 = new Car ("Red", 4);
var car2 = new Car ("Blue", 4);
Car1.showcolor ();
Car2.showcolor ();
</script>
All properties are created each time the new object is created, which means that multiple objects are completely independent, and
we define the purpose of the class to share the method and the data, But the Car1 object and the Car2 object are separate properties and functions, and
at least we should share the method. This is where the advantages of prototyping are.
3, prototype mode
<script>
Function Car () {}
Car.prototype.color= "red";
car.prototype.doors=4;
Car.prototype.drivers = new Array ("Tom", "Jerry");
Car.prototype.showcolor=function () {
alert (this.color);
}
//Call
var car1 = new Car ();
var car2 = new Car ();
Call.showcolor ();
Car2.showcolor ();
Alert (car1.drivers);
Car1.drivers.push ("Stephen");
Alert (car1.drivers);//Result: Tom,jerry,stephen
Alert (car2.drivers);//Result: Tom,jerry,stephen
// You can simplify the definition of prototype in JSON:
Car.prototype =
{
Color: "Red",
Doors:4,
Drivers: ["Tom", "Jerry", ' Safdad ' ],
Showcolor:function () {
alert (this.color);
}
</script>
The constructor for this code first, where there is no code, and then the properties of the car object are defined by adding properties through the object's prototype property.
This is a good approach, but the problem is that the object of car is pointing to the array pointer, and the two objects of car point to the same array of arrays, and when one of the objects car1 changes the reference (array) of the Property object, the other object car2 also changes, which is not allowed.
at the same time, the problem also manifests that the prototype cannot take any initialization parameters, causing the constructor to not initialize properly.
This requires a different approach: the Hybrid constructor/prototype pattern.
4. Mixed constructors, prototype patterns
<script>
function Car (color,doors) {
This.color = color;
this.doors = doors;
This.drivers=new Array ("Tom", "Jerry");
}
Car.prototype.showcolor=function () {
alert (This.color);
}
Call
var car1 = new Car (' Red ', 4);
var car2 = new Car (' Blue ', 4);
Car1.showcolor ();
Car2.showcolor ();
alert (car1.drivers);
Car1.drivers.push ("Stephen");
alert (car1.drivers); Results: Tom,jerry,stephen
alert (car2.drivers); Results: Tom,jerry
Alert (car1 instanceof Car);
</script>
JS defines a class or function