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.