summaryrefslogtreecommitdiffstats
path: root/src/ArrayPolyfill.js
blob: 19c1ede4130ac59d30e4522ab0a7542336dbd897 (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
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
    }
  }
})()