Skip to content

Instantly share code, notes, and snippets.

@bocharsky-bw
Last active August 29, 2015 13:57
Show Gist options
  • Save bocharsky-bw/9843792 to your computer and use it in GitHub Desktop.
Save bocharsky-bw/9843792 to your computer and use it in GitHub Desktop.
Create class with object constructor and public/private properties and methods.
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