Home > Web Front-end > JS Tutorial > 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
Release: 2016-05-16 18:40:51
Original
1062 people have browsed it
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.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template