JS Advanced (Classes and objects)

Source: Internet
Author: User


1. Factory mode
Factory mode refers to the creation of a factory function that returns a specific object type
eg.
function CreateObject () {
var object = new Object ();
Object.username = "Zhangsan";
Object.password = "123";

Object.get = function () {
Alert (This.username + "," + This.password);
}
return object;
}

var Object1 = CreateObject ();
var object2 = CreateObject ();

Object1.get ();
But the problem also comes, that is, each object is created, the method of the object is a new object, is a waste of resources;

2. How to construct a function

Similar methods:
eg.
function Car (scolor,idoors,impg) {
This.color=scolor;
This.doors=idoors;
This.mpg=impg;
This.showcolor=function () {
alert (This.color);
}
}
var ocar1=new Car ("Red", 4,23);
var ocar2=new Car ("Blue", 3,25);
Its problem is the same as above;

3. Prototype mode
This approach takes advantage of the object's prototype property and can be seen as a prototype on which to create a new object.

eg.
function person () {
Person.prototype.username = "Zhangsan";

Person.prototype.password = "123";

Person.prototype.getInfo = function (){

Alert (This.username + "," + This.password);
}

}

var person = new person ();
var person2 = new Person ();

Person.getinfo ();

Person2.getinfo ();

Disadvantages of using prototypes:

1. Cannot pass parameters;

2. All objects share properties and may cause program errors.

If you use a prototype to define an object, all of the generated objects share the properties in the prototype, so that an object changes the property and is reflected in other objects.

Simply using a prototype to define an object cannot assign an initial value to a property in a constructor, but only after the object has been generated to change the property values.

4. Hybrid constructor/prototype mode
eg.
function person (name) {
THIS.name = name;
}
Person.prototype.say = function () {
Alert ("I AM" +this.name);
}
var p1 = new Person ("Wang");
var p2 = new Person ("Li");
P1.say ();
P2.say ();
alert (P1.say==p2.say);
5. Dynamic prototype mode
In the constructor, the flags allow all objects to share a method, and each object has its own properties.
eg.

function person () {
This.username = "Zhangsan";
This.password = "123";

if (typeof Person.flag = = "undefined") {
Person.prototype.getInfo = function () {
Alert (This.username + "," + This.password);
}

Person.flag = true;

}
}

var p = new person ();
var p2 = new Person ();

P.getinfo ();
P2.getinfo ();

The properties between objects do not interfere with each other, and the same method is shared among objects.

JS Advanced (Classes and objects)

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.