对于此语法:

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


不知道为什么,在我看来,这合法。

最佳答案

大括号不是问题,问题是非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以及其余键的缩进。

关于javascript - Coffeescript:当我尝试创建对象文字的对象文字时出现意外的“{”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19640610/

10-13 00:44