Created
July 23, 2011 01:49
-
-
Save CrabDude/1100841 to your computer and use it in GitHub Desktop.
Node.js Array subclass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://gist.github.com/1321777