使用超级时,coffeescript中出乎意料的其他事情

问题描述 投票:1回答:1

我正在使用backbone.js,在coffeescript中编写它但得到此错误并且无法解决它!

代码片段:

module.exports = class CoreModel extends Backbone.Model

   destroyed: false

   # Helper to toggle the state of boolean value (using not)
   toggle: (key) -> @swap key, invert

   # Helper to change the value of an entry using a function.
   swap: (key, f) -> @set key, f @get key

   toJSON: -> if @destroyed then 'DESTROYED' else super

错误:

[stdin]:11:45: error: unexpected else
toJSON: -> if @destroyed then 'DESTROYED' else super
                                          ^^^^

不知道为什么这是一个意外的其他!

backbone.js coffeescript
1个回答
1
投票

如果您使用的是coffeescript 2,则需要在super()中使用括号。这里的错误信息应该更有帮助。

你可以在the docs上阅读它。

module.exports = class CoreModel extends Backbone.Model

  destroyed: false

  # Helper to toggle the state of boolean value (using not)
  toggle: (key) -> @swap key, invert

  # Helper to change the value of an entry using a function.
  swap: (key, f) -> @set key, f @get key

  toJSON: -> if @destroyed then 'DESTROYED' else super()

如果您发现需要旧行为的情况(所有参数都转发到super调用,那么您可以使用:

foo: -> super arguments...
© www.soinside.com 2019 - 2024. All rights reserved.