最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - remove CoffeeScript anonymous function calls - Stack Overflow

programmeradmin1浏览0评论
buildIMG = (src, resize) ->
  html  = '<div class="left"><div class="foods_image">'
  html += '<a onclick="popitup("" 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(\'\');" 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("" 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?

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?

Share Improve this question edited Apr 4, 2012 at 10:22 Teej asked Apr 4, 2012 at 10:02 TeejTeej 12.9k9 gold badges75 silver badges94 bronze badges 8
  • 1 If you don't understand what it does and why, learn about that first. It's nifty, idiomatic, and has good reasons. – user395760 Commented Apr 4, 2012 at 10:17
  • It puts the function in the global namespace from what I understand from the code. – Teej Commented Apr 4, 2012 at 10:27
  • Then you don't understand it. The whole point is not putting anything into the global namespace. – user395760 Commented Apr 4, 2012 at 10:35
  • Yes. sorry about that. I was thinking that this in the call was the window object? – Teej Commented Apr 4, 2012 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. – user395760 Commented Apr 4, 2012 at 10:53
 |  Show 3 more comments

2 Answers 2

Reset to default 19

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

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.

发布评论

评论列表(0)

  1. 暂无评论