Explain the constructor constructor pattern _javascript techniques in JavaScript

Source: Internet
Author: User

A simple description of the constructor pattern (see diagram):

Constructor constructor cannot be inherited, so overriding cannot be overridden, but can be overloaded overloading. constructor is used to create a specific type of object--Prepare the object for use, and receive parameters that the constructor can use to set the value of the member properties and methods when the object is first created

1. Create objects

Two ways to create new objects

var newobject={};
Var newobject=new object (); Concise notation of the//object builder

2. Basic constructor

JavaScript does not support classes in the case of objects and constructor, instantiate an object with the New keyword, the code is probably like this

function car (model,year,miles) {
This.model=model;
This.year=year;
This.miles=miles;
This.tostring=function ()
{return
this.model+ ' has done ' +this.miles+ ' Miles '
;}; Usage
//can create car new instance
var civic=new car ("Hona Civic", 2009,2000);
var mondeo=new car ("Ford Mondeo", 2010,5000);
Console.log (Civic.tostring ());
Console.log (Mondeo.tostring ());

3, with the prototype of the constructor

JavaScript has a prototype attribute. And when you invoke the JavaScript constructor to create an object, the new object has all the properties of the constructor prototype. In this way, you can create multiple car objects (access to the same prototype)

Funcion () Ca (model,year,miles) {
This.model=model;
This.year=year;
This.miles=miles;
Note that using Object.prototype.newMethod instead of Object.prototype is to redefine prototype objects
Car.prototype.tostring=function () { Return
this.model+ "has done" +this.miles+ "Miles";
}
;
//Usage
var civic=new car ("Honda Civic", 2009,20000);
var momdeo=new car ("Ford Mondeo", 2010,5000);
Console.log (Civic.tostring ());
Console.log (Mondeo.tostring ());

Now a single instance of ToString () can be shared between all car objects

Now, let's share the problem that the constructor constructor is wrong in the actual project

Class A {public
int Avar;
Public A () {
System.out.println ("AAA");
DoSomething ();
}
public void DoSomething () {
Avar = 1111;
System.out.println ("a.dosomething ()");
}
public class B extends A {public
int bvar = 2222;
Public B () {
System.out.println ("BBB");
DoSomething ();
System.out.println ("avar=" + Avar);
}
public void DoSomething () {
System.out.println ("bvar=" + Bvar);
}
public static void Main (string[] args) {
new B ();
}
}

The order is that the first generation of B will have to generate a, so call the constructor of a, output AAA, then call method DoSomething, Note: The method of A is covered by B, and you generate the object of B, so it calls the method of B, because Bvar is not currently given a value, so automatically initialize to 0 ;

Then generate the B object, initialize the variable Bvar, then call the constructor output BBB, and then invoke the method, at which point the Bvar is initialized, so the output bvar=2222, and the variable in object A is Avar the value 0 because the method dosomething of the object A is not invoked, the output 0

The full output is as follows:

Aaa
Bvar=0
Bbb
bvar=2222
Avar=0

Note: Initialization order, when inheriting, the gentleman becomes the superclass object, when the object is generated, the gentleman becomes a static variable, then a generic variable, and then the constructor is invoked! When all superclass objects are generated, the raw cost object, in the same order! When the method is overwritten, the method of the current object is invoked! This should be noted.

Related Article

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.