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
我目前有一个书签,它将JavaScript代码(上面的那个)插入网站。我写了上面的咖啡脚本,它生成了:删除CoffeeScript匿名函数调用
(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);
我剪掉了使用buildIMG的函数。该功能在网站上创建叠加层并显示该叠加层中的所有图像。为每个图像调用buildIMG来创建html。
问题是onclick="popitup("http://somewhere.com/test"
部分不起作用。它是未定义的。
的溶液我所做的就是删除此将其通过CoffeeScript的产生:
(function() {
}).call(this);
如我删除它被尽快固定。如何在生成的JavaScript中将CoffeeScript放入这些行中?
如果您不明白它的功能和原因,请先了解一下。它很漂亮,习惯用法,并且有很好的理由。 – delnan 2012-04-04 10:17:57
它把函数放在我从代码中理解的'global namespace'中。 – 2012-04-04 10:27:59
然后你就不明白了。整个观点是*不*将任何东西放入全局命名空间。 – delnan 2012-04-04 10:35:22