Last active
August 29, 2015 13:57
-
-
Save bocharsky-bw/9843792 to your computer and use it in GitHub Desktop.
Create class with object constructor and public/private properties and methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function MyClass(publicVal, privateVal) { | |
var self = this; | |
/** | |
* Public Property | |
*/ | |
this.publicValue = publicVal; | |
/** | |
* Public Property Setter | |
*/ | |
this.setPublicValue = function(publicVal) { | |
this.publicValue = publicVal; | |
return this; | |
} | |
/** | |
* Public Property Getter | |
*/ | |
this.getPublicValue = function() { | |
return this.publicValue; | |
} | |
/** | |
* Private Property | |
*/ | |
var privateValue = privateVal; | |
/** | |
* Private Property Setter | |
*/ | |
this.setPrivateValue = function(privateVal) { | |
privateValue = privateVal; | |
return this; | |
} | |
/** | |
* Private Property Getter | |
*/ | |
this.getPrivateValue = function() { | |
return privateValue; | |
} | |
/** | |
* Public Method | |
*/ | |
this.publicMethod = function() { | |
alert('Public Method'); | |
return this; | |
} | |
/** | |
* Public Method | |
*/ | |
var privateMethod = function() { | |
alert('Private Method'); | |
return this; | |
} | |
return this; | |
} | |
var obj = new MyClass('Public Value', 'Private Value'); | |
// or without new statement | |
var obj = MyClass('Public Value', 'Private Value'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment