Through the concept of functional inheritance, objects can be extended by using them as the context for a function call and assigning to this
.
However, this approach does not seem to work as expected when dealing with the Array
constructor.
var ctx = {
foo: "foo"
};
Array.call(ctx);
ctx
-> Object(foo: "foo")
In contrast, this method works with other constructor-like functions.
var fakeArrayConstructor = function () {
this.length = 5;
}
fakeArrayConstructor.call(ctx);
ctx
-> Object {foo: "foo", length: 5}
Is it possible that the Array
constructor does not assign some of its properties using this
, or is there another underlying reason? It's worth noting that much of the array functionality is stored in Array.prototype
, but that's not the main focus here.