Last active
June 1, 2023 17:27
-
-
Save YavorK/1f901511bfcae157cf4876186c02c0b1 to your computer and use it in GitHub Desktop.
how to get chained methods (builder pattern :3 ) in ES6
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
class math { | |
constructor() { | |
this.current = 0; | |
} | |
add(number) { | |
this.current += number; | |
return this; | |
} | |
multiply(number) { | |
this.current *= 5; | |
return this; | |
} | |
getCurrent() { | |
return this.current; | |
} | |
} | |
let funMath = new math; | |
console.log(funMath.getCurrent()); //0 | |
funMath | |
.add(5) | |
.multiply(5); | |
console.log(funMath.getCurrent()); //25 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment