Home  >  Article  >  Web Front-end  >  Javascript constructor, public, private privileges and static member definition methods_javascript skills

Javascript constructor, public, private privileges and static member definition methods_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:40:511044browse
Copy code The code is as follows:

//Constructor
function myClass(message)
{
//Public properties
this.myMessage = message;

//Private properties
var _separator = ' -';
var _myOwner = this;

//Private method
function showMessage()
{
alert(_myOwner.myMessage);
}

//Privileged method (also public method)
this. appendToMessage = function(appendMessage)
{
this.myMessage = _separator appendMessage;
showMessage();
}
}
//Public method
myClass.prototype.clearMessage = function()
{
this.myMessage = '';
}
myClass.prototype = {
clearMessage:function(){
this.myMessage = '';
}
}
//Static property
myClass.myName = 'SOBusiness';
//Static method
myClass.alertName = function()
{
alert (this.name);
}

Note: There are two methods used in the public method declaration part. In actual applications, one method is generally used. If there are two methods If both are used, attention should be paid to the order to prevent previously written methods from being cleared or overwritten.
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