JS Constructor (easy to understand, easy)

Source: Internet
Author: User
Tags access properties

* 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)

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.