BalaKrishnan웃
BalaKrishnan웃

Reputation: 4557

How to return multiple object form the js function

I have a function like below. When I create a new object of the function like this:

var newArray = new ArrayCollection ();

In the newArray I want to access the function property and class like this:

var first= newArray[0]

Instead of:

var first = newArray.Collections[0]

And:

newArray.add("a");

How to I modify the function to do this?

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

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

    }

}

Upvotes: 0

Views: 209

Answers (1)

arunes
arunes

Reputation: 3524

You can achieve like this

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])

Upvotes: 2

Related Questions