English 中文(简体)
咖啡文中的字词阵列阵列
原标题:Arrays of Literals in Coffeescript

当在联署材料中创建一系列字面文章时:

[{ name:  david , value:  blue  }, { name:  harold , value:  orange  }]

我唯一能用Coffeescript 写下来的方法就是:

[
  name:  david 
  value:  blue 
,
  name:  harold 
  value:  orange 
]

对我来说,这是非常丑陋的,“浮动的”逗号确实坐立不安。 对此,还有什么其他的语法吗?我知道,你可以继续用JS式的方法,从Coffeescript内进行,但我希望能更简洁一些。

最佳回答

这是每个咖啡开发商 似乎都遇到的一件事。

恐怕我们现在只有这些了 我唯一能想到的替代方案是:

[
  { name:  david , value:  blue  }
  { name:  harold , value:  orange  }
]

但它远非理想本身

如果有人想建议一种替代的、简洁的和CS-y的语法,我很乐意尝试在采摘器里执行它,并提出拉动请求。我很乐意有比这更好的。

问题回答

我记不起来为什么会这样, 或即使对你有用, 但这是你重新写的代码的另一种选择。

[
  {}= name:  david , value:  blue 
  {}= name:  harold , value:  orange 
]

< a href=" "http://coffeescription.org/#try:% 5B% 0A% 20%20B% 20% 7B%7D§20name:% 20% 27david%27,%20value:% 20%27Blue%27A%20A%20%20B7D20name:% 20%27harold%27,%20value:%2027oange%27Orange%270A%5D" rel="noreferr" 是,这似乎有效。

这似乎也很奇怪。我肯定有一个简单的理由,但我不确定。

[
  {}= 
    name:  david 
    value:  blue 
  {}= 
    name:  harold 
    value:  orange 
  {}= 
    name:  david 
    value:  blue 
]

< a href= http://coffeescription.org/ # try:% 5B% 0A% 20% 20% 20% 7B% 7D%20%00A%20%20%20%20 name:% 20% 27david% 27%0A%20%20%20%20%20%20%20%20%20%20%20# http://coffeescription.org/###try:% 20B%0%0A%20%20%20%20%20%20name:%20%20%20B%20%20:%20%20David%27%27B%20%20%20%20%20%20%20#name:%27Bavid%27Blue%20%20%20%20%20%20%20#20value:% 20%27blue%27blue%27%0A%05A%5D" rel="norefer%27r" > 见已编译。 <

要除去物体文字中的逗号,您可以选择:

[
  {
    name:  david 
    value:  blue 
  }, {
    name:  harold 
    value:  orange 
  }, {
    name:  david 
    value:  blue 
  }
]

或者,如果你真的恨恨"/他们"的逗号:

[
  {
    name:  david 
    value:  blue 
  }
  {
    name:  harold 
    value:  orange 
  }
  {
    name:  david 
    value:  blue 
  }
]

虽然我个人觉得浮动逗号 缩进一个层次 看上去不坏

[
    name:  david 
    value:  blue 
  , 
    name:  harold 
    value:  orange 
  , 
    name:  david 
    value:  blue 
]

我想当对象字的开始和完成 也几乎不可能忘记对象字的逗号=D

@Yuki Izumi问应如何执行(我没有直接评论他答复的因果报应) 。

我幼稚地尝试用咖啡文稿做的东西 是这样的:

arrayOfLiterals =
   somekey :  someval 
   otherkey :  otherval 
      keyInOtherArrayElement :  andItsVal 
      yetMoreInSecondElement :  andItsVal 
   thirdElement :  val 
      fourthElement :  val 

这是我的两分钱





相关问题
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][...

热门标签