gpt4 book ai didi

javascript - 如何从js函数返回多个对象

转载 作者:行者123 更新时间:2023-12-02 19:49:22 24 4
gpt4 key购买 nike

我有一个如下所示的功能。当我创建一个新的函数对象时,如下所示:

var newArray = new ArrayCollection ();

newArray 中,我想像这样访问函数属性和类:

var first= newArray[0]

而不是:

var first = newArray.Collections[0]

还有:

newArray.add("a");

如何修改函数来执行此操作?

ArrayCollection = function ()
{
this.Collections = new Array();

this.add = function ( value )
{
....
};
this.remove = function ( value )
{
....
};
this.insert = function ( indx, value )
{
....
};
this.clear = function ()
{ ...

}

}

最佳答案

你可以这样实现

ArrayCollection = function () {
this.Collections = new Array();
this.length = 0;

this.add = function (value) {
this[this.length] = value;
this.length++;
};

this.remove = function (value) {
// remove from array
this.length--;
};

this.insert = function (indx, value) {
// insert to array
this.length++;
};

this.clear = function () {
// clear array
this.length = 0;
};
};

var newArray = new ArrayCollection();
newArray.add('ll');
newArray.add('bb');
newArray.add('cc');

alert(newArray[0])
alert(newArray[1])
alert(newArray[2])

关于javascript - 如何从js函数返回多个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9496310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com