我有如下功能。当我像这样创建函数的新对象时:
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 ()
{ ...
}
}
答案 0 :(得分:2)
你可以这样实现
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])