summaryrefslogtreecommitdiffstats
path: root/src/ArrayPolyfill.js
blob: cf95d540888b8001da284857a45e77f95dab9679 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
    }
  }
})()