A brief analysis of JavaScript constructor function

Source: Internet
Author: User

constructor function


According to the investigation, when an object is instantiated by a constructor using the new operator, the following procedure is used:

    1. Creates an empty object.
    2. Points the __proto__ member of this empty object to the prototype member object of the constructor object.
    3. Assigns the scope of the constructor to the new object, so this in the constructor points to the new object, and then calls the constructor in the object context.
    4. Returns the newly created object.

Note : in the JavaScript standard, there is no __prop__ this attribute, but it is now the default standard property of some mainstream JavaScript execution environment, used to point to the prototype of the constructor. This property is not visible by default, and the details implemented in each execution environment are different, such as the property does not exist in IE. As long as we knowthatthere is a pointer to the constructor prototype inside the Javas Cript object, this pointer is automatically assigned when the new expression is called, and we should not modify it.

function Person (msg) {

Privileged properties (Public properties)
this.mymsg = msg; Can be called only in instances that have been instantiated
this.address = ' Dalian ';

Private properties
var name = ' Sumer ';
var age = 21;
var = this;

Private methods
function GetName () {
Alert (That.name);
}
Public methods, which can be publicly accessed externally, are re-constructed every time the instantiation is made, and prototype is a prototype share, all of which are instantiated together to refer to the same
This.getage = function () {
Alert (age); Private members can be accessed in public methods
}

Private and public members within the function, each instance created by the constructor will contain the same copy of the private and public members, so the more memory the instance consumes

And a private member is simply a constructor that runs under the object context when the object is created, and does not have a direct reference to the object.

}

Note : because of the scope characteristics of the JS function , these private members are shared by all public methods defined in the constructor, and are only shared by public methods defined in the constructor. This means that a class member defined in prototype will not be able to access a private member defined in the constructor. members).

Public method, for each instance of the object instantiated with the New keyword, adding a member to prototype will add the new method to the bottom of the constructor
Person.prototype.sayHello = function () {
Alert(' Hello world! ');
}
Static properties
A special instance of the object, which is the constructor itself that acts as an instance of a function object
Person.name = ' China ';
Static methods
Person.alertname = function () {
alert (this.name);
}
Instantiation of
var m1 = new Person (' me ');
----Test Properties----//
Console.log ( Person. name); China
Console.log (M1.name); Undefined, static properties do not apply to generic instances
Console.log (M1.constructor.name); China, to access the static property of the class, first accesses the instance's constructor, and then accesses the class's static property
Console.log (person.address); Undefined PersonThis in does not refer to the function itself, but to the object that calls address, and can only be an object
Console.log (m1.address); Dalian at this point refers to the M1 after the instantiation

----test method----//
Person.alertname (); China, class method for calling functions directly
M1.alertname (); FF:m1.alertname is isn't a function, Alertname is a method of the person class, and the instance object has no direct relation
M1.constructor.alertname (); China, calling the method (function) of the object constructor (class function)
M1.sayhello (); Hello everyone, the method under the prototype prototype of the MyObject class will be inherited by the instance
// Person. SayHello (); Person.sayhello is isn't a Function,sayhello is a prototype method, not a method of a class

----Test prototype----//
Console.log (M1.prototype); Undefined, the instance object does not have prototype
Console.log (Person.prototype); Object
alert (Person.prototype.constructor); Console.log returns the person (msg), at which time alert () is clearer, equal to person

Console.log (Person.prototype.constructor.name); China, equivalent to Person.name;

This analysis makes it clear that the use of private members is at the expense of code readability. And this implementation is more of a javascript technique, because it's not a mechanism that the language itself has. It should not be used too much in the construction of JS model. The use of public members can improve the readability of our code and make the model we build clearer.

A brief analysis of JavaScript constructor function

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.