Created
June 20, 2015 11:34
-
-
Save Sinewyk/9466b478d7003268c83b to your computer and use it in GitHub Desktop.
No more classes only proxy to functional stuff
This file contains hidden or 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
/** | |
* Functional is always better. Keep it simple, just proxy them to classes if you need | |
* Classes sucks. Try to avoid them. | |
* Always give a way to use something "class specific" to others | |
* => don't make it class specific in the first place | |
* / | |
function Foo(x) { | |
this.x = x; | |
} | |
// bad | |
Foo.prototype.squared = function() { | |
return this.x * this.x; | |
} | |
// OR | |
Foo.prototype.squared = function() { | |
this.x = this.x * this.x; | |
return x; | |
} | |
// better | |
// this lives "somewhere", you have commonjs and amd, deal with it. | |
function square(x) { | |
return x * x; | |
} | |
Foo.prototype.squared = function() { | |
return square(this.x); | |
} | |
// OR | |
Foo.prototype.squared = function() { | |
this.x = square(this.x); | |
return this.x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment