JS defines a class or function

Source: Internet
Author: User

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

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.