Public private privileged and static member usage analysis in JavaScript

  • 2020-03-30 04:20:48
  • OfStack

This article illustrates public, private, privileged, and static member usage in JavaScript. Share with you for your reference. Specific analysis is as follows:

The following content is in "JavaScript.DOM advanced programming" inside the excerpt out, more easy to understand, especially in this record, for the introduction of JavaScript friends to share.

//Constructor 
function myContructor(message){
this.myMessage = message;
//Private
var separator = ' -';
var myOwner = this;
//Private method
function alertMessage(){
alert(myOwner.myMessage);
}
alertMessage();
//Privileged methods (also public methods)
this.appendToMessage = function(string){
this.myMessage += separator + string;
alertMessage();
}
}
//Public method
myContructor.prototype.clearMessage = function(string){
this.myMessage = '';
}
//Static property
myContructor.name = 'Jankerli';
//Static method
myContructor.alertName = function(){
alert(this.name);
}

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

Because the private and privileged members are inside the function, they are brought to each instance of the function (that is, each instance created by the constructor contains a copy of the same private and privileged members, so the more instances take up memory).

2. The public stereotype member is part of the object blueprint and applies to each instance of the object instantiated through the new keyword.

3. Static members apply to only one particular instance of an object (which is the constructor itself as an instance of a Function object).


Related articles: