我已经编写了将其arrvar
变量设置为接收到的参数的函数。然后我为该函数添加了一些方法:
setArray
: 将参数添加到末尾arrvar
showArray
: 输出数组arrvar
showArrayType
: 输出数组的类型arrvar
(总是对象)showLength
: 输出数组的长度arrvar
这里的问题是,当我如下所示调用时,长度arrvar
不会增加。setArray()
为什么会这样?
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**