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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
/* 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
Arr.prototype.map = function ( fn ) {
const arr = new Arr()
arr.push.apply( arr, Array.prototype.map.call( this, fn ) )
return arr
}
return Arr
}
}
} )()
|