Talk about JS constructor function

Source: Internet
Author: User

//constructor Function//make your own objects replicate multiple times, and instances can access their internal properties and methods based on the level of access they set//when an object is instantiated, the constructor immediately executes any code it containsfunction MyObject (msg) {//Privileged Properties (public properties)     This. mymsg = msg;//can be called only in instances that have been instantiated     This. Address ='Chengdu'; //Private Property: cannot be accessed directly by object    varName ='Tirion'; varthat = This; //Private Methodsfunction Sayname () {alert (that.name); }    //Privileged methods (public methods)//can be publicly accessed externally//each instantiation of this method is reconstructed and prototype is a prototype share, all of which are instantiated together to refer to the same     This. Sayage =function () {alert (name);//Private members can be accessed in public methods        }    //private and privileged members within the function, each instance created by the constructor will contain copies of the same private and privileged members, so the more memory the instance consumes}//Public Methods//applies to 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 constructorMyObject.prototype.sayHello =function () {alert ('Hello everyone!'); }//Static Properties: Modified by an instance of an object, accessed by another instance of the object, as well as the modified value//A special instance of the object, which is the constructor itself that acts as an instance of a function objectMyobject.name =' China';//functions are also special objects, so you can add properties and methods to your functions. To access through objects, you first get the constructor, and then you access the properties of the constructor: M1.constructor.name//Static MethodsMyobject.alertname = function () {//access mode with static propertiesAlert This. name);//when the static method is accessed through the constructor of the object instance, the execution context is also constructor-MyObject, so the MyObject name is the    }//instantiation ofvarM1 =NewMyObject ('111'); 

The above is my understanding of the JS constructor ~ ~ ~

Talk about JS 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.