Coffeescript:当我尝试创建对象文字的对象文字时,意外的'{'

时间:2013-10-28 16:53:00

标签: javascript ruby-on-rails syntax coffeescript

对于这种语法:

  BASE_RUNNERS = {  basesLoaded:      { first: "manned",  second: "manned", third: "manned" },
                    firstAndSecond:   { first: "manned",  second: "manned", third: "empty"  },
                    firstAndThird:    { first: "manned",  second: "empty",  third: "manned" },
                    secondAndThird:   { first: "empty",   second: "manned", third: "manned" },
                    first:            { first: "manned",  second: "empty",  third: "empty"  },
                    second:           { first: "empty",   second: "manned", third: "empty"  },
                    third:            { first: "empty",   second: "empty",  third: "manned" },
                    empty:            { first: "empty",   second: "empty",  third: "empty"  }
                  }

我收到错误:

[stdin]:154:27: error: unexpected {
                          firstAndSecond:   { first: "manned",  second: "manned", third: "empty",   addedScore: 0 },
                          ^

不确定原因,对我来说看起来合法。

2 个答案:

答案 0 :(得分:3)

大括号不是问题,问题是非CoffeeScript缩进。 CoffeeScript对空白非常敏感,即使提供可选的括号,您仍需要注意缩进与所需的块结构相匹配。如果你这样写的话就会消失:

BASE_RUNNERS = {
  basesLoaded:      { first: "manned",  second: "manned", third: "manned" },
  firstAndSecond:   { first: "manned",  second: "manned", third: "empty"  },
  firstAndThird:    { first: "manned",  second: "empty",  third: "manned" },
  secondAndThird:   { first: "empty",   second: "manned", third: "manned" },
  first:            { first: "manned",  second: "empty",  third: "empty"  },
  second:           { first: "empty",   second: "manned", third: "empty"  },
  third:            { first: "empty",   second: "empty",  third: "manned" },
  empty:            { first: "empty",   second: "empty",  third: "empty"  }
}

您的难度来源是非缩进basesLoaded与其余键的缩进相结合。

答案 1 :(得分:1)

Coffeescript不接受有效的Javascript语法,我不得不重写它:

 BASE_RUNNERS =
      basesLoaded:    first: "manned", second: "manned", third: "manned"
      firstAndSecond: first: "manned", second: "manned", third: "empty"
      firstAndThird:  first: "manned", second: "empty", third: "manned"
      secondAndThird: first: "empty", second: "manned", third: "manned"
      first:          first: "manned", second: "empty", third: "empty"
      second:         first: "empty", second: "manned", third: "empty"
      third:          first: "empty", second: "empty", third: "manned"
      empty:          first: "empty", second: "empty", third: "empty"

我不确定哪个更好;看起来有点糟糕恕我直言。