CoffeeScript, prototypal inheritance and constructors

In CoffeeScript, it appears that the superclass constructor is not called when the subclass is instantiated.

Is there a way to get around this?

Here's an example:

class A
    element = null

    constructor: ->
        element = document.createElement "div"

    hide: =>
        element.style.display = "none"

class B extends A
    constructor: ->
        @hide() #error!

      

I would expect the constructor to be called first A

, then the constructor B

. If it B

then calls the method hide

, it should hide the element created in the constructor A

instead of saying it element

is null.

Thank!

+3


source to share


1 answer


I think you need to call super on the subclass



class A
    element = null

    constructor: ->
        element = document.createElement "div"

    hide: =>
        element.style.display = "none"

class B extends A
    constructor: ->
        super
        @hide() #error!

      

+5









All Articles