public
Last active — forked from CrabDude/SubArray.js

Node.js Array subclass

  • Download Gist
SubArray.js
JavaScript
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
// 20111028 jorge@jorgechamorro.com
// How to subclass Array
// In reply to https://gist.github.com/1100841/9c959db9314338a09c0f288c2c0ca5553816e400
 
function subArray () {
var nu= Array.apply(null, arguments);
nu.__proto__= subArray.prototype;
return nu;
}
 
subArray.prototype= {
__proto__: Array.prototype,
custom: function () { return "custom" }
}
 
/*
o= new subArray(1,2,3)
[ 1, 2, 3 ]
 
o instanceof Array
true
 
o instanceof subArray
true
 
o.custom()
'custom'
 
o.length
3
 
o.length=5, o
[ 1, 2, 3, , ]
 
o.length=2, o
[ 1, 2 ]
 
o.push(3), o
[ 1, 2, 3 ]
 
...etc
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.