JavaScript imitates object-oriented programming instance code (private, public variable ...) )

Source: Internet
Author: User

1 //define a JavaScript class2 functionJsclass (Privateparam,publicparam) {//constructor Function3     varPrimember = Privateparam;//Private Variables4      This. Pubmember = Publicparam;//Public Variables5     //Defining Private Methods6     functionPrimethod () {7         return"Primethod ()";8     }9     //defines a privileged method that can access all members of a privileged methodTen      This. Privilegedmethod =function(){ One         varstr = "This is the privileged method, I called \ n"; Astr + = "Private variable:" + primember + "\ n"; -str + = "Private method:" + primethod () + "\ n"; -str + = "public variable:" + This. pubmember + "\ n"; thestr + = "Public method:" + This. Pubmethod (); -         returnstr; -     } - } + //add public methods, cannot call private variables and methods -JsClass.prototype.pubMethod =function(){ +     return"Pubmethod ()"; A } at  - //using an instance of Jsclass -Jsobject =NewJsclass ("Primember", "Pubmember"); -  -alert (Jsobject.pubmember);//Popup pubmember Information - /* in alert (jsobject.primember);//popup Undefined information - alert (Jsobject.pubmethod ());//popup Pubmethod information to alert (Jsobject.primethod ());//Popup "object does not support this property or method" error + alert (Jsobject.privilegedmethod ()); - */

JavaScript imitates object-oriented programming instance code (private, public variable ...) )

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.