Quick review of fat arrow function binding.
Created
December 30, 2013 18:15
-
-
Save joyrexus/8185631 to your computer and use it in GitHub Desktop.
Fat-arrow example
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
{ok} = require 'assert' | |
class Greet | |
constructor: (@greeting) -> | |
thin: -> @greeting | |
fat: => @greeting # see http://coffeescript.org/#fat-arrow | |
greet = new Greet 'hi' | |
ok greet.thin() is 'hi' | |
ok greet.fat() if 'hi' | |
# run a function in different context, thereby changing `this`/@ | |
run = (f) -> f() | |
ok run(greet.thin) is undefined, 'since the thin arrow is context sensitive' | |
ok run(greet.fat) is 'hi', 'since the fat arrow preserves the original context' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment