small medium large xlarge

Back to: All Forums  CoffeeScript
01 Aug 2011, 14:54
Darcy Parker (1 post)


I bought the coffeescript book on Friday after reading a review, and I am enjoying what I’ve read so far.

On page 7, you give an example of compiling some coffeescript to javascript. I was/am intrigued by the pattern (function() { /** code **/ }).call(this);

It looks like a variant of the familiar IIFE pattern: (function(){ /* code */ })()

However, the .call(this) is new to me. I did a quick search online and found (It helps answer my question about the reason for the .call(this).)

I think it would be worthwhile to add some explanation in the coffeescript book about why coffeescript uses this pattern rather than use the traditional IIFE pattern. The “Javascript, Under Wraps” side topic explains things well, but for javascript programmers who are used the IIFE pattern, the .call(this) might be new. (like it was for me). Even a pointer to the answer about .call(this) would be helpful.


01 Aug 2011, 15:00
Trevor Burnham (53 posts)

The reason I didn’t put an explanation at that point in the book was that it precedes the explanation of scope (i.e. what @call@ and @this@ mean). Sidebars already break the flow of a book to some extent, so I thought it would be better to do a cursory sidebar that leaves some explanation for later rather than a lengthy sidebar that fills in every detail.

You must be logged in to comment