Skip to content

Clarify behaviour of constructor in class Foo extends Event {} #598

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

Closed
jakearchibald opened this issue Mar 14, 2018 · 14 comments
Closed

Clarify behaviour of constructor in class Foo extends Event {} #598

jakearchibald opened this issue Mar 14, 2018 · 14 comments
Labels
interop Implementations are not interoperable with each other needs tests Moving the issue forward requires someone to write tests topic: events

Comments

@jakearchibald
Copy link
Collaborator

jakearchibald commented Mar 14, 2018

class Foo extends Event {
  hello() { return 'hello'; }
}
const foo = new Foo('bar');
console.log(foo.constructor);

Chrome & Firefox say Foo, Edge & Safari say Event.

console.log(foo.hello());

Chrome & Firefox say hello, Edge & Safari throw (undefined is not a function).

The Chrome/Firefox behaviour is preferable, as it allows you to create custom event objects.

@jakearchibald
Copy link
Collaborator Author

@annevk annevk added needs tests Moving the issue forward requires someone to write tests interop Implementations are not interoperable with each other labels Mar 14, 2018
@annevk
Copy link
Member

annevk commented Mar 14, 2018

@domenic why is it again that we can just say "Interface() returns a new Interface" in prose and the subclassing falls out naturally? It seems a little weird.

@jakearchibald
Copy link
Collaborator Author

In Safari/Edge, it seems like new Event() is behaving like:

class Event {
  constructor() {
    return Object.create(Event.prototype);
  }
}

Which you could argue is correct according to:

event = new Event(type [, eventInitDict])
Returns a new event…

Although this is in a non-normative section. There aren't any normative steps for the event constructor.

@annevk
Copy link
Member

annevk commented Mar 14, 2018

There are: https://dom.spec.whatwg.org/#constructing-events. They seem to cover the inheritance aspect actually. But we don't cover it for EventTarget so it's still a little weird all around how to go about this.

@jakearchibald
Copy link
Collaborator Author

Ah, sorry. The constructor in the IDL links to the non-normative section.

@domenic
Copy link
Member

domenic commented Mar 14, 2018

This is all defined in the IDL for [Constructor]. I can find it when back at a computer.

@annevk
Copy link
Member

annevk commented Mar 14, 2018

https://heycam.github.io/webidl/#interface-object I guess, though I can't quite figure out where that deals with subclassing.

@annevk
Copy link
Member

annevk commented Mar 14, 2018

The constructor in the IDL links to the non-normative section.

It links nowhere in the IDL. The non-normative version links to the IDL though. That seems worth fixing.

@domenic
Copy link
Member

domenic commented Mar 14, 2018

Hmm yeah I guess that's broken right now. @tobie and I talked about how to fix it a month or so ago. (Roughly, instead of converting R to O, O should be created by ObjectCreate(new.target.prototype), and save R as an internal [[IDLValue]] slot.)

@annevk
Copy link
Member

annevk commented Mar 15, 2018

I filed #604 on [Constructor] not linking and whatwg/webidl#533 on [Constructor] not taking into account subclasses properly.

Bugs were already filed. Interested in adding a web-platform-tests test?

@jakearchibald
Copy link
Collaborator Author

I can write some tests for Event (and I'll check there's some for EventTarget). Or do we need to do something more general?

@annevk
Copy link
Member

annevk commented Mar 15, 2018

Just for Event for now I think (pretty sure EventTarget is covered). Thanks!

@jakearchibald
Copy link
Collaborator Author

Turns out it's already covered http://w3c-test.org/dom/events/Event-subclasses-constructors.html

@annevk
Copy link
Member

annevk commented Mar 15, 2018

\o/

@annevk annevk closed this as completed Mar 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interop Implementations are not interoperable with each other needs tests Moving the issue forward requires someone to write tests topic: events
Development

No branches or pull requests

3 participants