Analysis on the usage of public, private, privileged, and static members in JavaScript, and javascript static

Source: Internet
Author: User

Analysis on the usage of public, private, privileged, and static members in JavaScript, and javascript static

This article describes the usage of public, private, privileged, and static members in JavaScript. Share it with you for your reference. The specific analysis is as follows:

The following content is excerpted from "JavaScript. DOM advanced program design", which is easy to understand. Here we record it to help beginners share it with Javascript friends.
Copy codeThe Code is as follows: // Constructor
Function myContructor (message ){
This. myMessage = message;
// Private attributes
Var separator = '-';
Var myOwner = this;
// Private Method
Function alertMessage (){
Alert (myOwner. myMessage );
}
AlertMessage ();
// Privileged method (also public)
This. appendToMessage = function (string ){
This. myMessage + = separator + string;
AlertMessage ();
}
}
// Public Method
MyContructor. prototype. clearMessage = function (string ){
This. myMessage = '';
}
// Static attributes
MyContructor. name = 'jankerlil ';
// Static method
MyContructor. alertName = function (){
Alert (this. name );
}
Rules for public, private, privileged, and static members:

1. Because private and privileged members are inside the function, therefore, they are taken to every instance of the function (that is, each instance created by the constructor contains copies of the same private and privileged members, as a result, the more instances occupy the memory, the more ).

2. A public prototype member is part of an object blueprint. It is applicable to every instance of the object instantiated using the new keyword.

3. Static members are only applicable to one special instance of an object (this special instance is the constructor itself of the Function object instance ).

I hope this article will help you design javascript programs.

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.