Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CustomEvent could not be extended #3

Open
Stav88 opened this issue Jan 16, 2020 · 3 comments
Open

CustomEvent could not be extended #3

Stav88 opened this issue Jan 16, 2020 · 3 comments
Assignees

Comments

@Stav88
Copy link

Stav88 commented Jan 16, 2020

When I extends CustomEvent using babel the prototype is wrongly set to CustomEventPrototype instead of my class so I can't call methods of my class.

Scenario:

  • Extends CustomEvent using ES6 syntax:
export default class MyEvent extends CustomEvent {
    test() {return 'ok';}
}
  • Construct event and call test() methods:
const evt = new MyEvent();
console.log(Object.getPrototypeOf(evt));
console.log(evt.test());
  • Compile using Babel

Results:

[object CustomEventPrototype]
Object doesn't support property or method 'test'

Expected:

no error

[object MyEvent]
ok

Fix

I find a way to fix that by adding the following code at the end of CustomEvent contructor:

Object.setPrototypeOf(evt, Object.getPrototypeOf(this));
@kumarharsh
Copy link
Owner

Thanks for reporting this. Can you raise a PR for the same after checking what is the behaviour in browsers which do support CustomEvent? I'm on a mobile so can't check it myself right now.

@kumarharsh kumarharsh self-assigned this Jan 18, 2020
Stav88 added a commit to Stav88/custom-event-polyfill that referenced this issue Jan 20, 2020
kumarharsh#3
Need work for Edge and Webkit where constructor work but not the extends
@Stav88
Copy link
Author

Stav88 commented Jan 20, 2020

I have checked the behaviour of actual browsers without polyfill :

  • Gecko (Firefox): support CustomEvent constructor and extends
  • Bink (Chrome): support CustomEvent constructor and extends
  • Webkit (Safari): support CustomEvent constructor but doesn't support correctly extends
  • Edge: support CustomEvent constructor but doesn't support correctly extends
  • IE: don't support CustomEvent constructor

@kumarharsh
Copy link
Owner

Thanks. I'll take a look at your commit in some time...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants