ES5 example for event-target-shim
HTML
<script src="https://rawgit.com/mysticatea/event-target-shim/master/dist/event-target-shim.umd.js"></script>
<h1>ES5 example for event-target-shim</h1>
<hr>
<p>See the console of F12 tool.</p>
JavaScript
var EventTarget = EventTargetShim.EventTarget
var defineEventAttribute = EventTargetShim.defineEventAttribute
// Define a derived class.
function Foo() {
EventTarget.call(this)
}
Foo.prototype = Object.create(EventTarget.prototype, {
constructor: { value: Foo, configurable: true, writable: true }
// ...
})
// Define `foo.onhello` property.
defineEventAttribute(Foo.prototype, "hello")
// Register event listeners.
var foo = new Foo()
foo.addEventListener("hello", function(e) {
console.log("hello", e)
})
foo.onhello = function(e) {
console.log("onhello", e)
}
// Dispatching events
function isSupportEventConstrucor() { // IE does not support.
try {
new CusomEvent("hello")
return true
} catch (_err) {
return false
}
}
if (isSupportEventConstrucor()) {
foo.dispatchEvent(new CustomEvent("hello", { detail: "detail" }))
} else {
var e = document.createEvent("CustomEvent")
e.initCustomEvent("hello", false, false, "detail")
foo.dispatchEvent(e)
}