0

对于这种语法:

  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 },
                          ^

不知道为什么,在我看来它是合法的。

4

2 回答 2

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与其余键的缩进相结合。

于 2013-10-28T17:35:01.247 回答
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"

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

于 2013-10-28T17:25:44.227 回答