According to this answer I want to create my own subclass of Array
QArray = function() {
Array.apply(this, arguments);
};
QArray.prototype = new Array();
QArray.prototype.constructor = QArray;
which works as expected, method invocation works, but the constructor does not chain to Array.
// test constructor chaining
q = new QArray('1', '2', '3');
assert.equals('1', q[0]); // => undefined
assert.equals(3, q.length); // => 0
q = new QArray(10);
assert.equals(10, q.length); // => 0
This tests pass if I replace QArray
with plain Array
. Somehow Array
seems to be a special case. (I run this in Rhino 1.6 which is Javascript 1.5.) How can I fix my custom subclass of Array?