2

これは機能します:

class Foo 
  class @_Bar 
    @narf = ''
    @point : ->
      @narf = 'what'

  class @_Baz extends @_Bar 
    @point : ->
      @narf = 'woo'
      super()

これはしません

class Foo 
  class @_Bar 
    @narf = ''
    @point = ->
      @narf = 'what'

  class @_Baz extends @_Bar 
    @point = ->
      @narf = 'woo'
      super()

実行Foo._Baz.point()するとスローされ、エラーが発生します。


ここで何が起こっているのか誰か説明してください。

4

3 に答える 3

3

私にはコンパイラのバグのようです。書き込み

class X
  @classMethod: ->

class X
  @classMethod = ->

同等である必要がありますがsuper、2 つのメソッド間で異なる方法でコンパイルされます。最初は、正しくコンパイルされます。

X.__super__.constructor.classMethod.apply(this, arguments);

classMethod2 番目の例では、インスタンス メソッドのようにコンパイルします。

X.__super__.classMethod.apply(this, arguments);
于 2013-11-08T17:49:35.410 に答える
2

これは機能します:

class Foo 
  class @_Bar 
    @narf = ''
    point : ->
      @narf = 'what'

  class @_Baz extends @_Bar 
    @point = ->
      @narf = 'woo'
      super()

alert Foo._Baz.point()  # 'what'
alert new Foo._Bar().point() # 'what'

つまり、コンパイル済み@point= superは instance を指すことになりますpoint:。その JS は:_Baz.__super__.point.call(this)です_Bar.prototype.point.call(this)。(extends定義: child.__super__ = parent.prototype)。

過去の Coffeescript の変更から、それ@point:が静的 (クラス) メソッドの意図された構文である (そしてコンパイラ自体でそのように使用される) ことは明らかです。

于 2013-11-10T03:58:40.817 に答える