【发布时间】:2012-04-18 00:46:47
【问题描述】:
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 代码(上述代码)插入网站。我写了上面的咖啡脚本,它生成了这个:
(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 放入这些行?
【问题讨论】:
-
如果您不明白它的作用和原因,请先了解它。它很漂亮、惯用,而且有充分的理由。
-
根据我对代码的理解,它将函数放在
global namespace中。 -
那你就不懂了。重点是不将任何东西放入全局命名空间。
-
是的。对于那个很抱歉。我在想调用中的
this是窗口对象? -
不一定。如果整个代码都放在全局范围内。否则(例如在方法内部调用),它会从周围的作用域继承
this。