4
buildIMG = (src, resize) ->
  html  = '<div class="left"><div class="foods_image">'
  html += '<a onclick="popitup("http://somewhere.com/test" href="javascript:void(0)">'
  html += '   <img src="'+src+'" '+resize+' />'  
  html += '</a>'
  html += '</div></div>'
  html

popitup = (url) ->
  newwindow=window.open(url,'name','height=640,width=640')
  newwindow.focus() if window.focus
    false

I currently have a bookmarklet that inserts javascript code(the one above) into a website. I wrote the above coffeescript and it generates this:

(function() {
  var buildIMG, popitup;

  buildIMG = function(src, resize) {
    var html, nbsp;
    html = '<div class="left"><div class="foods_image">';
    html += '<a onclick="popitup(\'http://somewhere.com/test\');" href="javascript:void(0)">';
    html += '   <img src="' + src + '" ' + resize + ' />';
    html += '</a>';
    html += '</div></div>';
    return html;
  };
  popitup = function(url) {
    var newwindow;
    newwindow = window.open(url, 'name', 'height=640,width=640');
    return newwindow.focus()(window.focus ? false : void 0);
  };
}).call(this);

I snipped the functions that uses buildIMG. That function creates an overlay over the site and displays all images in that overlay. buildIMG is called for each image to create the html.

The problem is that the onclick="popitup("http://somewhere.com/test" portion doesn't work. It is undefined.

A solution I did was to remove this which was generated by CoffeeScript:

(function() {

}).call(this);

It was fixed as soon as I removed that. How do I not have CoffeeScript put in those lines in my generated javascript?

Teej
  • 12,764
  • 9
  • 72
  • 93
  • 1
    If you don't understand what it does and why, learn about that first. It's nifty, idiomatic, and has good reasons. –  Apr 04 '12 at 10:17
  • It puts the function in the `global namespace` from what I understand from the code. – Teej Apr 04 '12 at 10:27
  • Then you don't understand it. The whole point is *not* putting anything into the global namespace. –  Apr 04 '12 at 10:35
  • Yes. sorry about that. I was thinking that `this` in the call was the window object? – Teej Apr 04 '12 at 10:48
  • Not necessarily. It is if the whole code is placed in the global scope. Otherwise (e.g. called inside a method), it inherits `this` from the surrounding scope. –  Apr 04 '12 at 10:53
  • possible duplicate of [Pattern for CoffeeScript modules](http://stackoverflow.com/questions/5211638/pattern-for-coffeescript-modules) – Trevor Burnham Apr 04 '12 at 17:55
  • @TrevorBurnham, I don't see how that question could be a duplicate of mine. I have a specific question for my problem and he is asking for `Pros and Cons of anonymous functions`. – Teej Apr 08 '12 at 23:17
  • @ThorpeObazee OK, it would be more accurate to call it a duplicate of [Getting rid of CoffeeScript's closure wrapper](http://stackoverflow.com/questions/5693211/getting-rid-of-coffeescripts-closure-wrapper). But the answers on the previously mentioned question go into more depth as to why the wrapper exists and what alternatives there are to disabling it. – Trevor Burnham Apr 08 '12 at 23:32

2 Answers2

19

CoffeeScript allows to compile JavaScript without this safety wrapper by --bare option.

zbynour
  • 19,747
  • 3
  • 30
  • 44
5

Although suppressed within this documentation for clarity, all CoffeeScript output is wrapped in an anonymous function: (function(){ ... })(); This safety wrapper, combined with the automatic generation of the var keyword, make it exceedingly difficult to pollute the global namespace by accident.

It's from CoffeScript site. If you want to create a global method or variable you need to

root = this
localProperty = "111"
root.property = localProperty

And then you'll get a property in global scope.

Flops
  • 1,410
  • 15
  • 18
  • +1 Great answer and I would have accepted this but the answer to my question was answered by @zbynour – Teej Apr 04 '12 at 11:18
  • +1 this is useful, for example, in Rails when you're not compiling in the command line and don't want to disable it for every coffee script file – comandante N Oct 02 '13 at 20:39