Constructor pattern _ JavaScript skills in javascript

Source: Internet
Author: User
Constructor cannot be inherited, so Overriding cannot be overwritten, but Overloading can be overloaded. This article will share with you the Constructor mode of the constructor in JavaScript. If you are interested in Constructor, learn about the constructor mode and briefly describe the Constructor mode (see the figure below ):

Constructor cannot be inherited, so Overriding cannot be overwritten, but Overloading can be overloaded. The constructor is used to create objects of specific types-prepare objects for use and receive parameters that can be used by the constructor to set the value of member attributes and methods when creating objects for the first time.

1. Create an object

Two methods for creating a new object

Var newObject ={}; var newObject = new object (); // simple record of the object constructor

2. Basic Constructor

When Javascript does not support classes, objects and Constructor instantiate an object using the new keyword. The code is 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 // you can create a new car 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. Constructor with prototype

JavaScript has the prototype attribute. And after calling the JavaScript constructor to create an object, the new object will have all attributes of the constructor prototype. In this way, you can create multiple Car objects (access the same prototype)

Funcion () Ca (model, year, miles) {this. model = model; this. year = year; this. miles = miles; // pay attention to Object. prototype. newMethod instead of Object. prototype is used to redefine the prototype object 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 among all Car objects.

The following is an example of constructor's mistakes in actual projects.

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 as follows. First, generate B and then convert it to A. Therefore, call the constructor of A and output AAA, and then call the dosomething method. Note: This method of A is overwritten by B, you generate the object of B, so it calls the method of B. Because BVAR does not currently have a given value, it is automatically initialized to 0;

Then generate the B object, first initialize the variable BVAR, then call the constructor to output BBB, and then call the method. At this time, BVAR has been initialized, so the output is BVAR = 2222, the AVAR variable in object A does not call the dosomething method of object A, so its value is 0, and 0 is output.

The output is as follows:

AAA
Bvar = 0
BBB
Bvar= 2222
Avar = 0

Note: The initialization order. When the class is inherited, the class is a super class object. When the class is generated, the class is a static variable, then a general variable, and then a constructor is called! After all the superclass objects are generated, the cost objects are generated in the same order! When the method is overwritten, call the method of the current object! Note This.

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.