Revisions
-
xk revised this gist
Oct 28, 2011 . 1 changed file with 35 additions and 20 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,27 +1,42 @@ // 20111028 [email protected] // 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 */ -
CrabDude revised this gist
Jul 23, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ // 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; -
CrabDude revised this gist
Jul 23, 2011 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ // For context on difficulties of subclassing Array: // See, http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/ var sandbox = {SubArray: null}; require('vm').createScript('SubArray = Array').runInNewContext(sandbox); -
CrabDude created this gist
Jul 23, 2011 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ // For context on difficulties of subclassing Array, See, http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/ var sandbox = {SubArray: null}; 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...