Manual this binding with IIFEs in Coffeescript

I recently had an issue where I wanted to access the outer scope of a function, while also using the this scope inside the function.

Often in Javascript developers will do something like

that = this

obj.doThing(function() {

allowing you to access the outerscope and the inner scope. In coffeescript (and ES6) this functionality can be accessed using the => (fat arrow) function

obj.doThing =>

If you look at the compiled output, coffescript builds an IIFE (Immediately invoked function expression) that looks like this

  return function() {

As you can see it binds the outer this to _this inside the function, replacing @ (this) with _this

As I found out though, in CS 1.9, you can’t use @ and this in a fat arrow function because it will replace both with @ and this with _this but what if I want to do both like in the first example?

Well, the answer is anti-climatic, but basically I did what coffeescript does manually using the coffeescript do block.

Which look this:

function: do (outerScope = @) ->
    anotherFunction(@value, outerScope.get 'someValue')

of course, the bind function function.bind or _.bind would work here as well.