Like Ruby, CoffeeScript uses lexical scope. This means that a variable that is declared inside a function is invisible to outside code. From there on, a variable lives inside the scope in which a value is assigned to it without being able to be redeclared. Because we don't have access to the var keyword, we can't simply declare a variable and use it in a different scope without assigning a value to it.
Consider the following code:
The output might come as a surprise to you, but it actually makes a lot of sense. A variable that is declared inside an inner scope cannot shadow a variable with the same name that is declared in an outer scope. I highly recommend that you avoid reusing variable names as shown by this example.
When calling a constructor function using the new operator, this references the new object being created. We've also explicitly added 'return @' in order to return the new object. Otherwise the function assigned to the download property is implicitly returned. Let’s move on to the next case.
As I already mentioned in a previous blog post, we can reuse methods from other objects using the call()/apply() methods defined on the prototype of Function. In this example we reuse the download function in the context of the otherPodcast object. This means that this now references otherPodcast instead of the podcast object. Let’s dive into the final and most common scenario.
Here this simply references the current object to which the executing function belongs to.
Going over these different scenarios regarding the function context, it might be useful to be able to define a function that binds to the current value of this. In CoffeeScript we can use the => symbol instead of –> to accomplish just that. Let’s look at the code example from the CoffeeScript website.
This is especially helpful for event callbacks. The reason you might want to consider the ‘fat arrow’ in this case is that callbacks are executed in the context of the element, i.e. this references the element. When you want to keep this equal to the local context, without doing the self = this dance, then ‘fat arrows’ are the way to go.
As you can see, splats don't have to be defined at the end of the parameter list. For this example, the first and last value get to be assigned to the regular parameters after which the remaining values in the middle of the list get assigned to the splat parameter. When we only specify two values, then these will be used for the regular parameters while the splat parameter will be null.
The values for the regular parameters get to be assigned first after which the remaining values all get to be passed to the splat parameter. This also implies that there can be only a single splat parameter for a given function definition.
Splats are not only available for function definitions, but also for calling functions as well.
someFunction = -> podcast = 'Hardcore history'
podcast = 'Astronomy cast'
console.log podcast # This outputs 'Astronomy cast'
No doubt that CoffeeScript brings a lot of goodness to the table when it comes to functions. But there’s also a lot more. Stay tuned for the next couple of blog posts.
Until next time.