3 ms·
You are correct, once again, all properties were available both by property name (if used in an associative context) and by numeric index. Since we assigned 'le
by masswerk 6y ago
You are correct, once again, all properties were available both by property name (if used in an associative context) and by numeric index. Since we assigned 'length' already in the constructor, we would have to accommodate for this, i.e., start counting at 1.
Turns out, my recollections are a bit on the rosy side… :-)
Edit: This should work:
function MyArray() {
this.length = 0;
}
function arrayPop() {
return this[this.length--];
}
function arrayPush( value ) {
return this[++this.length] = value;
}
var a = new MyArray();
a.pop = arrayPop;
a.push = arrayPush;
a.push('a');
a.push('b');
alert( a[1] ); // 'a'
for (var i = 1; i <= a.length; i++) alert( a[i] );
at which point it is much simpler to do:
function MyArray() {};
var a = new MyArray(), aLength = 0;
a[aLength++] = 'a';
a[aLength++] = 'b';
for (var i = 0; i < aLength; i++) alert( a[i] );
var b = new MyArray(), bLength = 0;
// etc
which is also, what was actually used.