Last active
April 3, 2016 23:27
-
-
Save thurt/ac5528347275c2236bdc106fb58d411d to your computer and use it in GitHub Desktop.
an example of how to implement an immutable counter
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 Counter { | |
constructor(start_value) { | |
this.plus = this.plus(start_value) | |
} | |
plusFn(num, _num) { | |
var res = num + _num | |
this.plus = (_num) => { | |
return this.plusFn(res, _num) | |
} | |
return res | |
} | |
plus(num) { | |
return (_num) => { | |
return this.plusFn(num, _num) | |
} | |
} | |
} | |
var myCounter = new Counter(1) | |
console.log(myCounter.plus(1)) // 2 | |
console.log(myCounter.plus(1)) // 3 | |
console.log(myCounter.plus(3)) // 6 | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment