small medium large xlarge

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

Hi,

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 http://stackoverflow.com/questions/5211638/pattern-for-coffeescript-modules (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 stackoverflow.com answer about .call(this) would be helpful.

Darcy

Trevor_burnham__goofy_headshot__pragsmall
01 Aug 2011, 15:00
Trevor Burnham (54 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