Javascript constructor Analysis

Source: Internet
Author: User

Javascript constructor Analysis

Constructor


According to the survey, when an object is instantiated by using the new operator through the constructor, the process is as follows:

  • Create an empty object.
  • Point the _ proto _ member of this empty object to the prototype member object of the constructor object.
  • Assign the constructor scope to the new object. Therefore, this in the constructor points to the new object, and then calls the constructor in the context of the object.
  • Returns the newly created object.

    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 scope 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.

  • 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.