0

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 ()
    {   ...

    }

}
1
  • Sorry, can't understand your question. Answer to title: just return a list return [firstObject, secondObject];. Answer to the rest of the post (it seems to be pretty unrelated to title): make your collection class use Array as prototype, or alter Array's prototype as Emissary suggests. (By the way, please consider accepting some answers people provided to your other questions.) Commented Feb 29, 2012 at 9:46

1 Answer 1

2

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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.