Created
October 25, 2011 15:25
-
-
Save xavierm02/1313126 to your computer and use it in GitHub Desktop.
A way to "subclass" Array. It will fail if you add something at an index > the length of the array. Doesn't provide Array as [[Class]] either.
This file contains 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
var FakeArray = ( function ( ) { | |
function FakeArray( ) { | |
var fakeArray = Object.create( FakeArray.prototype ); | |
var realArray = Array.apply( Object.create( Array.prototype ), arguments ); | |
Object.defineProperty( fakeArray, '', { | |
value: realArray | |
} ); | |
forwardIndexesUntil( realArray.length ); | |
return fakeArray; | |
} | |
var prototypeOfFakeArray = FakeArray.prototype = Object.create( Array.prototype, { | |
constructor: { | |
value: FakeArray, | |
configurable: true, | |
writable: true | |
}, | |
length: { | |
get: function ( ) { | |
return this[ '' ].length; | |
}, | |
set: function ( value ) { | |
this[ '' ].length = value; | |
forwardIndexesUntil( value ); | |
}, | |
configurable: true, | |
enumerable: true | |
} | |
} ); | |
function forwardIndex( index ) { | |
Object.defineProperty( prototypeOfFakeArray, index, { | |
get: function ( ) { | |
return this[ '' ][ index ]; | |
}, | |
set: function ( value ) { | |
this[ '' ][ index ] = value; | |
forwardIndexesUntil( index + 1 ); | |
}, | |
configurable: true | |
} ); | |
} | |
function forwardIndexesUntil( index ) { | |
for ( var i = index; i > greatestForwardedIndex; --i ) { | |
forwardIndex( i ); | |
} | |
} | |
var greatestForwardedIndex = 0; | |
forwardIndex( 0 ); | |
var prototypeOfArray = Array.prototype; | |
var propertyDescriptor; | |
for ( var name in prototypeOfArray ) { | |
if ( prototypeOfArray.hasOwnProperty( name ) ) { | |
propertyDescriptor = Object.getOwnPropertyDescriptor( prototypeOfArray, name ); | |
propertyDescriptor.value = ( function ( value ) { | |
return function ( ) { | |
return value.apply( this[ '' ], arguments ); | |
}; | |
}( ) ); | |
Object.defineProperty( prototypeOfFakeArray, name, propertyDescriptor ); | |
} | |
} | |
return FakeArray; | |
}( ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well <| isn't flagged strawman anymore. It's officially harmony. So it'll come with the rest of harmony.
And there is kind of no discussion about how it should work, I'm quite sure they decided how it was going to work and agreed. If I remember well, they're just debating about the symbol (some don't like <|).
On the contrary, on Proxies, they know they want proxies but not how exactly. So they kind of sent them in the wild to see what would happen.
If you take a look at the end of the page, you can see that Proxies have already changed a lot, whereas <| is quite fixed.
(Do not consider everything I say as true, I'm speaking of things I read / heard in english without always paying the required attention [they speak wait too much on es-discuss -.- ] and I might have misunderstood since it's not my native language)
http://wiki.ecmascript.org/doku.php?id=harmony:proxies