//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