JavaScript object-oriented and prototype (1): Constructor
We are no stranger to constructor. In the Object-Oriented field, constructor is already a common problem. Learning again in JavaScript is really a kind of feeling.
1. Simple Review
Constructor is a special method. It is mainly used to initialize an object when an object is created, that is, assigning an initial value to the object member variable. It is always used together with the new operator in the statement for creating an object. A special class can have multiple constructors, which can be distinguished based on the number of parameters or different parameter types, that is, the overload of constructors.
C # Constructor (a simple example ):
Public class Cat () {private string name =; public Cat () // without parameters, this is the default {this. name = anonymous;} public Cat (string name) // defined constructor with the {this. name = name ;}}
Since constructors are used when creating functions, how can we create functions in JavaScript?
Ii. Create an object using JavaScript
1. Use new to create an object and check the following code:
Var box = new Object (); // create an Object box. name = 'javascript '; // create a name attribute and assign a value to box. runtime = 100; // create an age attribute and assign a value to box. run = function () {// create a run () method and return the return value "return this. name + this. runtime + 'running... ';}; alert (box. run (); // output attribute and method value
This will cause a problem. When you want to create a similar class, a large number of repeated code will appear. To solve multiple similar object declarations, you can use the factory mode. Here we can think back to the factory in the design mode to solve the problem of how to instantiate objects.
Function createObject (name, age) {// The var obj = new Object (); obj. name = name; obj. age = age; obj. run = function () {return this. name + this. age + 'running... ';}; return obj;} var box1 = createObject ('lil', 100); // var box2 = createObject ('jack', 200) of the first instance ); // The Second Instance alert (box1.run (); alert (box2.run (); // keep independent
Ii. Playing the constructor:
The factory mode is used to solve the problem that a large number of duplicates are generated for the instantiated object. However, it is impossible to figure out which object they actually belong to, that is, the recognition problem. This leads to the constructor.
ECMAScript can use Constructor (constructor) to create specific objects. Object type.
Function Box (name, age) {// constructor mode this. name = name; this. age = age; this. run = function () {return this. name + this. age + 'running... ';};} var box1 = new Box ('lil', 100); // new Box () var box2 = new Box ('jack', 200 );
The constructor method differs from the factory mode method as follows:
1. Create object (newObject () not displayed in the constructor method ());
2. directly assign attributes and methods to this object;
3. There is no explain urn statement.
Constructor methods have some specifications:
1. The function name and the instantiated constructor name are the same and in upper case (PS: not mandatory, but this write will help distinguish between the constructor and common function );
2. Create an object through the constructor. The new operator must be used.
The new Object () execution process is as follows:
1. When the constructor and the new Constructor () are used, the new Object () is executed in the background ();
2. Assign the scope of the constructor to the new Object (the Object created by the new Object ().
Indicates the new Object.
3. Execute the code in the constructor;
4. Return the new object (directly returned in the background ).
(3) Determine whether the internal methods of the constructor are of the basic type or reference type.
Use the above constructor Box () to make the following comparison:
Var box1 = new Box ('lil', 100); // pass consistent var box2 = new Box ('lil', 100 ); // same as alert (box1.name = box2.name); // true, the attribute value is equal alert (box1.run () = box2.run (); // true, the method value is equal, because the passing parameter is consistent with alert (box1.run = box2.run); // false, the method is actually a reference address.
After box1 and box2 are instantiated, their addresses are different. Although the value of the run () method is the same, false is returned when the referenced addresses of their methods are compared. This proves that the internal method of the constructor is of the reference type.
To ensure the consistency of the referenced address, you can bind the same function to the constructor.
function Box(name, age) { this.name = name;
this.age = age;
This. run = run;} function run () {// make sure that the referenced address is the same. return this. name + this. age + 'running ...';}
Although the global function run () is used to solve the problem of consistent reference addresses, this method brings about a new problem, the global this is Box itself when the object is called, and this represents window when called as a common function. This leads to the uncertainty of this.