Skip to content

Instantly share code, notes, and snippets.

@rfelix
Last active January 2, 2016 19:49
Show Gist options
  • Save rfelix/8352579 to your computer and use it in GitHub Desktop.
Save rfelix/8352579 to your computer and use it in GitHub Desktop.
Thoughts on separating declaration + definition in javascript
// Usage:
// var test = new Test(dep1, dep2);
// test.publicMethod();
function Test(dep1, dep2) {
var object = new TestClass();
object.init(dep1, dep2);
return object;
}
function TestClass() {
this.dep1 = undefined;
this.dep2 = undefined;
this.privateVar = '123';
this.privateMethod = function() {
// ...
}
}
TestClass.prototype.init = function(dep1, dep2) {
this.dep1 = dep1;
this.dep2 = dep2;
}
TestClass.prototype.publicMethod = function() {
this.privateMethod();
return this.privateVar;
}
// Usage:
// var test = new Test().init(dep1, dep2);
// test.publicMethod();
// Or even:
// new Test().init(dep1, dep2).publicMethod();
function Test() {
this.dep1 = undefined;
this.dep2 = undefined;
this.privateVar = '123';
this.privateMethod = function() {
// ...
}
}
Test.prototype.init = function(dep1, dep2) {
this.dep1 = dep1;
this.dep2 = dep2;
return this;
}
Test.prototype.publicMethod = function() {
this.privateMethod();
return this.privateVar;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment