Created
February 17, 2013 18:43
-
-
Save darkoverlordofdata/4972731 to your computer and use it in GitHub Desktop.
Create callable object class
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
class Counter | |
constructor: (max) -> | |
count = 0 | |
counter = () -> | |
if (count < counter.max) then return ++count | |
else return count = 1 | |
counter.max = Math.abs(parseInt(max)) || 1 | |
counter.__proto__ = Counter:: | |
# Setting the internal [[proto]]` property of the function being returned to the prototype of the constructor. | |
return counter | |
# Allow `instanceof Function` to return `true`, as well as `call`, `apply`, etc to be used. | |
Counter:: = () -> | |
# Reset the `constructor` property of the prototype to point to the constructor `Counter`. | |
#Counter::constructor = Counter | |
Counter::reset = () -> | |
while @() < @max | |
i=0 | |
counter = new Counter(3) | |
console.log(counter()) # 1 | |
counter.reset() | |
console.log(counter()) # 1 | |
console.log(counter()) # 2 | |
counter.reset() | |
console.log(counter()) # 1 | |
console.log(counter()) # 2 | |
console.log(counter()) # 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment