I have written function which sets its arrvar variable to the received arguments.
Then I have added some methods to that function:
setArray: adds the arguments to the end of thearrvarshowArray: outputs the arrayarrvarshowArrayType: outputs the type of arrayarrvar(which is always object)showLength: outputs the length of the arrayarrvar
The problem here is that the length of the arrvar does not increment when I call setArray() as shown below. Why is this so?
function ArrayClass()
{
if (arguments.length != 0) {
this.arrvar = arguments;
}
}
ArrayClass.prototype.setArray = function () {
for (var i = this.arrvar.length, j = 0; j < arguments.length; i++, j++)
{
this.arrvar[i] = arguments[j];
}
}
ArrayClass.prototype.showArray = function ()
{
for (var i in this.arrvar) {
document.writeln(this.arrvar[i]+' ');
}
document.writeln("<br />");
}
ArrayClass.prototype.showArrayType = function ()
{
document.writeln(typeof this.arrvar + '<br />');
}
ArrayClass.prototype.showLength = function()
{
if (this.arrvar) {
document.writeln('Length: ' + this.arrvar.length + "<br />");
}
}
var arrObj = new ArrayClass(11, 22, 33, 44);
arrObj.showArray(); //11 22 33 44
arrObj.showArrayType(); //object
arrObj.showLength(); //4
arrObj.setArray(55, 66, 77);
arrObj.showArray(); //11 22 33 44 55 66 77
arrObj.showArrayType(); //object
arrObj.showLength(); //**4**