Skip to content

Instantly share code, notes, and snippets.

@xk
Forked from CrabDude/SubArray.js
Created October 28, 2011 06:59

Revisions

  1. xk revised this gist Oct 28, 2011. 1 changed file with 35 additions and 20 deletions.
    55 changes: 35 additions & 20 deletions SubArray.js
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,42 @@
    // For context on difficulties of subclassing Array:
    // See, http://perfectionkills.com/how-ecmascript-5-still-does-not-allow-to-subclass-an-array/
    // 20111028 [email protected]
    // How to subclass Array
    // In reply to https://gist.github.com/1100841/9c959db9314338a09c0f288c2c0ca5553816e400

    var sandbox = {};
    require('vm').createScript('SubArray = Array').runInNewContext(sandbox);
    function subArray () {
    var nu= Array.apply(null, arguments);
    nu.__proto__= subArray.prototype;
    return nu;
    }

    var SubArray = sandbox.SubArray;
    console.log(SubArray);
    SubArray.prototype.__proto__ = {
    subArray.prototype= {
    __proto__: Array.prototype,
    custom: function () { return "custom" }
    }

    // Your sandboxed array prototype properties here.
    custom: function() {
    console.log('custom');
    }
    };
    /*
    o= new subArray(1,2,3)
    [ 1, 2, 3 ]
    var x = new SubArray(1,2,3);
    o instanceof Array
    true
    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]
    o instanceof subArray
    true
    // etc...
    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
    */
  2. @CrabDude CrabDude revised this gist Jul 23, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SubArray.js
    Original 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 = {SubArray: null};
    var sandbox = {};
    require('vm').createScript('SubArray = Array').runInNewContext(sandbox);

    var SubArray = sandbox.SubArray;
  3. @CrabDude CrabDude revised this gist Jul 23, 2011. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion SubArray.js
    Original 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/
    // 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);
  4. @CrabDude CrabDude created this gist Jul 23, 2011.
    26 changes: 26 additions & 0 deletions SubArray.js
    Original 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...