【问题标题】:unexpected else in coffeescript when using super使用超级时咖啡脚本中的意外其他
【发布时间】:2019-08-06 04:00:43
【问题描述】:

我正在使用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】:

    如果您使用的是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...
    

    【讨论】:

    • 非常感谢@Caffeinated.tech
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 2014-03-15
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    相关资源
    最近更新 更多