Closed
Description
Bound methods are now only bound after calling the super constructor. If the super constructor calls any methods in the class, you end up with unbound references to functions.
Example:
class SomeView extends Backbone.View
initialize: ->
# Can't use @callback here - initialize is called from the super constructor
someFunc: ->
# It's *probably* safe to use @callback here - but it would suddenly get unsafe
# if we (or the super class or a derived class) decided to call @someFunc() in
# initialize later on.
callback: => # ...
While it's not possible to bind the functions before calling the super constructor, there are some ways CS2 could make bound methods "safer" to use. Some ideas:
- In classes with inheritance, always bind references to bound methods where they're used. In the example above,
@callback
would always compile tothis.callback.bind(this)
. While this solves the issue for local uses, it doesn't help for external access. - Don't allow bound methods in classes with inheritance. Safest option, but not great.
- Rename the bound method and only set the proper name when binding
this
. Doesn't solve the issue, but would make errors like in the example above more visible. Would need additional thought to avoid naming conflicts and for interaction with non-CoffeeScript classes in the inheritance chain.
Related issue #4239 still stands, but now it's also an issue for the base class.