WebReflection Ltd
webreflection / event-target Goto Github PK
View Code? Open in Web Editor NEWThe EventTarget Class Polyfill.
License: ISC License
The EventTarget Class Polyfill.
License: ISC License
I installed the module via npm, required it as said in the docs (Browserify 16.2.2 handles the bundling).
const EventTarget = require('event-target').default;
but I get an error:
Safari 11.1 (doesn't have EventTarget constructor)
TypeError: undefined is not a constructor (evaluating 'new EventTarget')
FF 61 (has support, works perfect w/o polyfill)
TypeError: EventTarget is not a constructor
Including the script tag from the CDN works though.
Seen in Safari 11.1
Works fine in Chrome 66.0.3359.139
Using browser event target implementation:
let eventTarget;
eventTarget = document.createElement('div');
eventTarget.addEventListener('foo', () => console.log(1), {once: true});
eventTarget.addEventListener('foo', () => console.log(2), {once: true});
eventTarget.dispatchEvent(new CustomEvent('foo'))
1
2
Using polyfill:
eventTarget = new EventTarget;
eventTarget.addEventListener('foo', () => console.log(1), {once: true});
eventTarget.addEventListener('foo', () => console.log(2), {once: true});
eventTarget.dispatchEvent(new CustomEvent('foo'))
1
Using polyfill without once option:
eventTarget = new EventTarget;
eventTarget.addEventListener('foo', () => console.log(1));
eventTarget.addEventListener('foo', () => console.log(2));
eventTarget.dispatchEvent(new CustomEvent('foo'))
1
2
I use the following with webpack to import the polyfill:
import EventTarget from 'event-target/esm/index.js';
Currently when requiring in CJS, (according to documentation) one has to do:
const EventTarget = require('event-target').default;
It'll be way more natural if it's:
const EventTarget = require('event-target');
Is this fixable?
If I see correctly support for ES3 engines is not ensured.
(so Every. JavaScript. Engine.
statement is not true ;-))
While it's not common to write for ES3 engines these days. It still can be the case in some scenarios, and EventTarget
is core interface to many functionalities.
As now both FF and Chrome provide EventTarget
as normal extensible class, wouldn't it be good to return it by default in those engines?
See step 2.7 of https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
Unfortunately, I don't know off the top of my head what the best way to report the exception would be.
console.error
Following evaluate to false when setting window.EventTarget using the polyfill
import EventTarget from 'event-target/esm/index.js'
console.log(EventTarget instanceof window.EventTarget);
import EventTarget from 'event-target/esm/index.js'
console.log(document.createElement('div') instanceof EventTarget);
Not sure if it is possible to get either of these to evaluate to true..?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.