Why do we need apply method inside the constructor to invoke any method defined on the prototype object?
Code working:
function Test(){
this.x = [];
this.add.apply(this,arguments);
}
Test.prototype.add = function(){
for(var i=0; i < arguments.length; i++){
this.x.push(arguments[i]);
}
}
var t = new Test(11,12)
t.x //[11,12] this is fine
t.x.length //2 this is also fine
But when i directly call add inside constructor
Code not working:
function Test(){
this.x = [];
this.add(arguments);
}
Test.prototype.add = function(){
for(var i=0; i < arguments.length; i++){
this.x.push(arguments[i]);
}
}
var t = new Test(11,12);
t.x.length; //1 Not added all elements why?