Public, private, privileged, and static members in JavaScript

Source: Internet
Author: User

The following content is in the "Javascript.dom Advanced Program Design" excerpt out of it, I think it is easy to understand, afraid of later will forget or confused, especially in this record, but also with you are getting started JavaScript friends to share ah.

  code is as follows copy code

//constructor
function mycontructor (message) {
This.mymessage = message;
/ /private Property
var separator = '-';
var myowner = this;
//Private method
Function Alertmessage () {
Alert (myowner.mymessag e);
}
Alertmessage ();
//Privileged Method (also public method)
This.appendtomessage = function (string) {
This.mymessage = Separator + string;
Alertmessage ();
}
}
//public method
MyContructor.prototype.clearMessage = function (string) {
This.mymessage = ';
}
//static properties
Mycontructor.name = ' Jankerli ';
//static method
Mycontructor.alertname = function () {
Alert (this.name );
}

About public, several rules for private, privileged, and static members:
1, because private members and privileged members are inside the function, they are taken to each instance of the function (that is, each instance created by the constructor contains a copy of the same private and privileged member). As a result, more instances consume more memory.
2, a publicly-owned stereotype member is part of the object's blueprint and applies to each instance of the object instantiated with the New keyword.
3, static members apply only to a particular instance of an object (this particular instance is the constructor itself as a function object instance).

Related Article

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.