Binding of CoffeeScripts Fat Arrow
Again this is a frequently asked questions on StackOverflow. I have already answered it there however I think its hard to find.So I will reproduce it here again.
When does the fat arrow bind?
The fat arrow binds at 3 occasions- when declaring a method within a class
- when declaring a function within a method or function
- when declaring a function in global context
Declaring a method within a class
When the Coffeescript compiler encouters the following syntactical pattern within a class declarationSo regardless which context you call somemethod it will allways use the creating instance as the execution context.
When declaring a function within a method or function
When declaring a function in global context
If you define a free floating function (meaning as a method in a class and not within another function/method) just like thisThe important part however is that this is now always the global context of your execution environment. If you are in the browser it will be the window object. If you are running node.js it will be the module you are just running.
Warning: You shouldn't define any function accessing your global context anyway. This calls for trouble.
Did you find this entry useful? Please upvote it on StackOverflow.