Skip to content

Illegal Invocation when calling super methods of subclass ancestors #1

Open

Description

@shennan

It seems the context of the object is lost somewhere in calling an ancestor's methods.

class A extends HTMLElement {
  empty () {
    while (this.hasChildNodes()) {
      this.removeChild(this.lastChild)
    }
  }
}
class B extends A {
  constructor () {
    super()
    this.empty()
  }
}

customElements.define('b-element', B)

fixBabelClass(A)
fixBabelClass(B)

let b = new B()

As an illustration, note how Chrome prints my b custom element when transpiling/fixing-class, as opposed to native ES6 Chrome.

Babel + Fix-Babel-Class
screen shot 2017-06-06 at 17 24 09

Native ES6
screen shot 2017-06-06 at 17 23 01

Activity

  1. WebReflection commented on Jun 6, 2017

    @WebReflection
    Owner

    try this instead:

    class B extends A {
      constructor () {
        super().empty();
      }
    }
  2. WebReflection commented on Jun 6, 2017

    @WebReflection
    Owner

    actually, no.

    There are few things here that could go wrong.

    To start with the first one, you cannot patch after registering the class, it's impossible to overwrite the registry.

    this is upside down

    customElements.define('b-element', B)
    
    fixBabelClass(A)
    fixBabelClass(B)

    do this instead

    fixBabelClass(A)
    fixBabelClass(B)
    
    customElements.define('b-element', B)

    Secondly, if you are not using a polyfill upfront nothing will ever work.

    You need document-register-element or any other polyfill that works to use Babel and custom elements because these need to patch HTMLElement.

    If you have done both things and you still have problems please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions