Javascript constructor analysis, javascript Constructor
Constructor
According to the survey, when an object is instantiated by using the new operator through the constructor, the process is as follows:
Note:There is no _ prop _ attribute in the JavaScript standard, but it is now a default standard attribute of some mainstream JavaScript Execution environments, which is used to point to the constructor prototype. This attribute is invisible by default and has different implementation details in different execution environments. For example, this attribute does not exist in IE browser. We only need to know that a pointer to the constructor prototype exists in the Javascript Object. This pointer is automatically assigned when the new expression is called, and we should not modify it.
Function Person (msg ){
// Privileged attributes (Public attributes)
This. myMsg = msg; // can be called only in the instance after being instantiated
This. address = 'dalian ';
// Private attributes
Var name = 'sumer ';
Var age = 21;
Var that = this;
// Private Method
Function getName (){
Alert (that. name );
}
// The public method can be publicly accessed by the outside. This method must be re-constructed every time it is instantiated, and prototype is prototype sharing. After all instantiation, the same
This. getAge = function (){
Alert (age); // you can access private members in public methods.
}
// Private and Public members are inside the function. Each instance created by the constructor contains copies of the same private and public members. Therefore, the more instances, the more memory occupied.
// The Private member is only executed by the constructor in the object context when the object is created, and does not have direct reference to the object.
}
Note:Due to the closure feature of JS functions, these private members are shared by all public methods defined in the constructor and are only shared by the public methods defined in the constructor. This means that the class members defined in prototype cannot access the Private Members defined in the constructor.Member ).
// Public method: applies to every instance of the object instantiated using the new keyword. Adding a member to prototype adds the new method to the bottom layer of the constructor.
Person. prototype. sayHello = function (){
Alert ('Hello world! ');
}
// Static attributes
// Special instance for objects, that is, the constructor itself used as the Function object instance
Person. name = 'China ';
// Static method
Person. alertname = function (){
Alert (this. name );
}
// Instantiate
Var m1 = new Person ('me ');
// ---- Test attributes ----//
// Console. log (Person. name); // china
// Console. log (m1.name); // undefined. Static attributes are not applicable to general instances.
// Console. log (m1.constructor. name); // china, to access the static attributes of the category class, first access the constructor of the instance, and then access the static attributes of this class.
// Console. log (Person. address); // undefined. this in Person refers not to the function itself, but to the address object, and can only be an object.
// Console. log (m1.address); // Dalian this indicates the m1
// ---- Test method ----//
// Person. alertname (); // china, directly call the class method of the Function
// M1.alertname (); // FF: m1.alertname is not a function. alertname is a method of the Person class and has no direct relationship with the instance object.
// M1.constructor. alertname (); // china, call this object constructor (class function) method (function)
// M1.sayHello (); // hello everyone, the method under prototype of the myObject class will be inherited by the instance
// Person. sayHello (); // Person. sayHello is not a function. sayHello is a prototype method, not a class method.
// ---- Test prototype ----//
// Console. log (m1.prototype); // undefined, the instance object does not have prototype
// Console. log (Person. prototype); // Object
// Alert (Person. prototype. constructor); // the console. log returns Person (msg). At this time, alert () is clearer, which is equivalent to Person
// Console. log (Person. prototype. constructor. name); // china, equivalent to Person. name;
From this analysis, we can clearly understand that using private members is at the cost of code readability. In addition, this implementation is more of a JavaScript technique, because it is not a mechanism of the language itself. Too many applications should not be used in JS model building. The use of public members can improve the readability of our code and make our model clearer.
What is the javascript constructor?
Javascript itself does not have the concept of classes, but only functions. The javascript class is actually a javascript function. In this special function, it can contain variables and references of other javascript Functions. The special function itself is the so-called class constructor of javascript.
Question about javascript Object Constructor
This is a reserved javascript word and cannot be used as a variable.
In addition, this in the constructor refers to baidu in var baidu = new createObj ().
To implement your functions,
Function createObj (urlPath, innerStr ){
This. href = urlPath
This. innHTML = innerStr
}
CreateObj. prototype. createHTML = function (){
Var a = document. createElement ("");
A. href = this. href
A. innerHTML = this. innHTML
Document. body. appendChild ();
}
Var baidu = new createObj ("www.baidu.com", "baidu ")
Baidu. createHTML ()