English 中文(简体)
咖啡帽中的 Lo子吗?
原标题:Loop in coffee script?

I m translating a piece of code from javascript to coffeescript.

  for (var i = 0, len = keys.length; i < len; i++) {
    k = keys[i];
    if (!mailOptions.hasOwnProperty(k))
      mailOptions[k] = app.set( mailOptions )[k]
  }

我不知道如何对待这一问题,在专题网站上也不清楚,有人能否给我解释? 非常感谢!

最佳回答
for key in keys
  if not mailOptions.hasOwnProperty key
    mailOptions[key] = (app.set  mailOptions )[key]

Or guard-style:

for key in keys when not mailOptions.hasOwnProperty key
  mailOptions[key] = (app.set  mailOptions )[key]

:

var key, _i, _len;

for (_i = 0, _len = keys.length; _i < _len; _i++) {
  key = keys[_i];
  if (!mailOptions.hasOwnProperty(key)) {
    mailOptions[key] = (app.set( mailOptions ))[key];
  }
}
问题回答

http://js2coffee.org/“rel=“nofollow”http://js2coffee.org/:

i = 0
len = keys.length

while i < len
  k = keys[i]
  mailOptions[k] = app.set("mailOptions")[k]  unless mailOptions.hasOwnProperty(k)
  i++

But I wouldn t do it this way. I would just do:

for k in keys
  mailOptions[k] = app.set("mailOptions")[k] unless mailOptions.hasOwnProperty k

This outputs the following (excluding var, which it also outputs):

for (_i = 0, _len = keys.length; _i < _len; _i++) {
  k = keys[_i];
  if (!mailOptions.hasOwnProperty(k)) {
    mailOptions[k] = app.set("mailOptions")[k];
  }
}

Or, if you wanted to be fancier, which I don t advise in this situation, since it sacrifices some readability:

(mailOptions[k] = app.set("mailOptions")[k] unless mailOptions.hasOwnProperty k) for k in keys




相关问题
How do I define global variables in CoffeeScript?

On Coffeescript.org: bawbag = (x, y) -> z = (x * y) bawbag(5, 10) would compile to: var bawbag; bawbag = function(x, y) { var z; return (z = (x * y)); }; bawbag(5, 10); compiling via ...

I m trying to re-write this in CoffeeScript. Coming unstuck

function getElementsByClassName(className) { // get all elements in the document if (document.all) { var allElements = document.all; } else { var allElements = document.getElementsByTagName("...

Anonymous functions syntax in CoffeeScript

I ve been looking at CoffeeScript and I m not understanding how you would write code like this. How does it handle nested anonymous functions in its syntax? ;(function($) { var app = $....

Why are my CoffeeScript/backbone.js events not firing?

I m trying to familiarize myself with CoffeeScript and backbone.js, and I must be missing something. This CoffeeScript: MyView = Backbone.View.extend events: { "click" : "testHandler" ...

Custom compiler with maven

I m trying to make Maven2 to compile coffeescript to javascript. As far as I m concerned there is no plugin which provides compiling coffeescript. Is there a compiler-plugin for maven which can be ...

what is wrong when I compile my .coffee files

Hi there Iam using coffeeScript for my apps now and I love it but recently I ve been having a lot of trouble with compilation, Iam using it for a rails application and when I run coffee -w -c public/...

CoffeeScript on Windows?

How can I try CoffeeScript on Windows? The installation instructions are only for *nix: http://jashkenas.github.com/coffee-script/#installation EDIT: Since I asked this a while ago, many new ...

How can I compile CoffeeScript from .NET?

I want to write an HttpHandler that compiles CoffeeScript code on-the-fly and sends the resulting JavaScript code. I have tried MS [JScript][1] and IronJS without success. I don t want to use [Rhino][...

热门标签