English 中文(简体)
消除咖啡经典匿名功能
原标题:remove CoffeeScript anonymous function calls
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);

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.

问题在于<代码>onclick=“popitup(”http://some where.com/test"t t work。 它没有定义。

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

(function() {

}).call(this);

一旦我撤走,就立即确定。 我怎么说,咖啡没有在我的 generated子里把这些话写进来?

最佳回答

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

之后,你在全球范围获得财产。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签