Adding elements to an array does not increments its length

goldsexygirl

New Member
I have written function which sets its \[code\]arrvar\[/code\] variable to the received arguments.Then I have added some methods to that function:
  • \[code\]setArray\[/code\]: adds the arguments to the end of the \[code\]arrvar\[/code\]
  • \[code\]showArray\[/code\]: outputs the array \[code\]arrvar\[/code\]
  • \[code\]showArrayType\[/code\]: outputs the type of array \[code\]arrvar\[/code\] (which is always object)
  • \[code\]showLength\[/code\]: outputs the length of the array \[code\]arrvar\[/code\]
The problem here is that the length of the \[code\]arrvar\[/code\] does not increment when I call \[code\]setArray()\[/code\] as shown below. Why is this so?\[code\] 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 = arguments[j]; } } ArrayClass.prototype.showArray = function () { for (var i in this.arrvar) { document.writeln(this.arrvar+' '); } 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**\[/code\]
 
Back
Top