Home  >  Article  >  Web Front-end  >  Usage analysis of public, private, privileged and static members in JavaScript_javascript skills

Usage analysis of public, private, privileged and static members in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:30:581114browse

The examples in this article describe the usage of public, private, privileged and static members in JavaScript. Share it with everyone for your reference. The specific analysis is as follows:

The following content is excerpted from "JavaScript.DOM Advanced Programming", which is relatively easy to understand. I record it here so that friends who are getting started with Javascript can share it.

Copy code The code is as follows:
//Constructor
function myContructor(message){
this.myMessage = message;
//Private attribute
var separator = ' -';
var myOwner = this;
//Private method
function alertMessage(){
alert(myOwner.myMessage);
}
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);
}

A few rules about public, private, privileged and static members:

1. Since private members and privileged members are inside the function, they will be brought to each instance of the function (that is, each instance created by the constructor will contain a copy of the same private and privileged members , so the more instances, the more memory it takes up).

2. Public prototype members are part of the object blueprint and are applicable to every instance of the object instantiated through the new keyword.

3. Static members only apply to a special instance of the object (this special instance is the constructor itself as a Function object instance).

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn