* Constructor Function:
* 1. The function name of the constructor is best capitalized (otherwise the Webstorm editor will prompt an error)
* 2. Multiple copies of your own objects
* 3. Constructors can create public properties, public methods, private properties, and private methods
* 3. Methods inside the constructor can access properties inside the constructor
* 4. When an object is instantiated, the constructor immediately executes all of the code it contains
* 5. Private methods and private properties are not exposed, even if the instantiated object of the constructor is created, it is still inaccessible and can only be accessed inside the constructor
1 /**2 * Constructor Function:3 * 1. The function name of the constructor is best capitalized (otherwise the Webstorm editor will prompt an error)4 * 2. Multiple copies of your own objects5 * 3. Constructors can create public properties, public methods, private properties, and private methods6 * 3. Methods inside the constructor can access properties inside the constructor7 * 4. When an object is instantiated, the constructor immediately executes all of the code it contains8 * 5. Private methods and private properties are not exposed, even if the instantiated object of the constructor is created, it is still inaccessible and can only be accessed inside the constructor9 *Ten * */ One A functionMyObject (msg) { - //Create a public property - This. mymsg =msg; the This. Address = ' Shanghai '; - -Console.log (' Inside constructor '); - + //Create public methods - This. Sayage =function () { +Console.log (' This is a public method '); A }; at - //Private Properties - varname = ' Liuqiuchen '; - varAge = 100; - var_this = This; - in //Private Methods - /** Private methods and private properties are not exposed, even if the instantiated object of the constructor is created, it is still inaccessible and can only be accessed inside the constructor*/ to functionMyFunc () { +Console.log (' I am a private method, the name is: ' +name); - } the * This. Run =function () { $ MyFunc ();Panax Notoginseng }; - the This. run2 =function () { +Console.log ("MSG:" +_this.mymsg); A }; the } + - //instantiating a constructor object $ varObj1 =NewMyObject (' information ')); $ - obj1.sayage (); - Obj1.run (); theObj1.run2 ();
JS Constructor (easy to understand, easy)