Public, private, privileged, and static members in JS

Source: Internet
Author: User

Public members: Members defined by modifying the prototype attribute of the constructor can be accessed by all instances of the constructor;

Private Members: Construct the Members defined by books (VaR for attributes and function for functions), which can only be accessed within the constructor;

Static members: Members directly defined on the object can only be accessed by this object;

Privileged Member: A member defined by this in the constructor. The access permission is the same as that of a public member;

 

The following example shows the Members in this set.

// Constructor <br/> function myconstructor (Message) <br/>{< br/> This. mymessage = message; </P> <p> // Private Attribute <br/> var separator = '-'; <br/> var myowner = this; </P> <p> // Private method <br/> function alertmessage () <br/> {<br/> alert (myowner. mymessage); // if this is an instance that represents the private method, rather than an instance of the myconstructor constructor, you cannot use <br/>}< br/> alertmessage (); </P> <p> // privileged method <br/> This. appendtomessage = function (string) <br/>{< br/> This. mymessage + = separator + string; <br/> alertmessage (); <br/>}< br/> // public method <br/> myconstructor. prototype. clearmessage = function (string) <br/>{< br/> This. mymessage = ''; <br/>}< br/> // static attribute <br/> myconstructor. name = 'jeff '; <br/> // static method <br/> myconstructor. alertname = function () <br/>{< br/> alert (this. name); <br/>}

 

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.