Skip to content

Instantly share code, notes, and snippets.

@CrabDude
Created July 23, 2011 01:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CrabDude/1100841 to your computer and use it in GitHub Desktop.
Save CrabDude/1100841 to your computer and use it in GitHub Desktop.
Node.js Array subclass
// For context on difficulties of subclassing Array:
// See, http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/
var sandbox = {};
require('vm').createScript('SubArray = Array').runInNewContext(sandbox);
var SubArray = sandbox.SubArray;
console.log(SubArray);
SubArray.prototype.__proto__ = {
__proto__: Array.prototype,
// Your sandboxed array prototype properties here.
custom: function() {
console.log('custom');
}
};
var x = new SubArray(1,2,3);
x instanceof SubArray // true
x instanceof Array // true
x.length = 5
x.push(5) // x = [1,2,3,null,null,5]
x.length = 2 // x = [1,2]
[4,5,6].concat(x) // [4,5,6,1,2,3]
// etc...
@xk
Copy link

xk commented Oct 28, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment