diff options
Diffstat (limited to 'src/types/ArrayPolyfill.js')
-rw-r--r-- | src/types/ArrayPolyfill.js | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/types/ArrayPolyfill.js b/src/types/ArrayPolyfill.js new file mode 100644 index 0000000..cf95d54 --- /dev/null +++ b/src/types/ArrayPolyfill.js @@ -0,0 +1,30 @@ +/* eslint no-new-func: "off" */ +export const subClassArray = (function () { + try { + // try es6 subclassing + return Function('name', 'baseClass', '_constructor', [ + 'baseClass = baseClass || Array', + 'return {', + '[name]: class extends baseClass {', + 'constructor (...args) {', + 'super(...args)', + '_constructor && _constructor.apply(this, args)', + '}', + '}', + '}[name]' + ].join('\n')) + } catch (e) { + // Use es5 approach + return (name, baseClass = Array, _constructor) => { + const Arr = function () { + baseClass.apply(this, arguments) + _constructor && _constructor.apply(this, arguments) + } + + Arr.prototype = Object.create(baseClass.prototype) + Arr.prototype.constructor = Arr + + return Arr + } + } +})() |