Detailed description of Constructor mode in JavaScript, constructor
The constructor mode is described in the following figure ):
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.
Articles you may be interested in:
- JavaScript constructor and instanceof, a happy family in JSOO
- Constructors and constructor attributes of Javascript
- JavaScript class and inherited constructor attributes
- Constructor attributes after javascript new
- Understanding the implementation principle of Javascript_11_constructor
- Analysis on actual functions of js constructor
- In-depth analysis of constructor and prototype in js
- Several important attributes in JavaScript (this, constructor, prototype)
- Summary of prototype and constructor in JavaScript