Skip to content

Commit

Permalink
Fix CustomEvent extends for IE
Browse files Browse the repository at this point in the history
kumarharsh#3
Need work for Edge and Webkit where constructor work but not the extends
Stav88 authored Jan 20, 2020
1 parent 813029a commit 20623a2
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions polyfill.js
Original file line number Diff line number Diff line change
@@ -18,6 +18,20 @@
throw new Error('Could not prevent default');
}
} catch (e) {
function copyProtoypeToObject(obj, proto) {

This comment has been minimized.

Copy link
@kumarharsh

kumarharsh Jan 20, 2020

Since this function is only used for IE10, can you move this to where it's actually used (in the else part? Otherwise we are creating a function which won't be used for 99.5% of the browsers.

if (proto === window.CustomEvent.prototype) {
return;
}

// eslint-disable-next-line no-restricted-syntax
Object.getOwnPropertyNames(proto).forEach((key) => {
const descriptor = Object.getOwnPropertyDescriptor(proto, key);
if (descriptor && !Object.prototype.hasOwnProperty.call(obj, key)) {
Object.defineProperty(obj, key, descriptor);
}
});
}

var CustomEvent = function(event, params) {
var evt, origPrevent;
params = params || {};
@@ -44,6 +58,12 @@
this.defaultPrevented = true;
}
};

if (Object.setPrototypeOf) { // IE 11, Edge and Webkit
Object.setPrototypeOf(evt, Object.getPrototypeOf(this));
} else { // IE 10
copyProtoypeToObject(evt, Object.getPrototypeOf(this));
}
return evt;
};

0 comments on commit 20623a2

Please sign in to comment.