0

I am trying to make something like this:

var Test = 
{
    A:function()
    {
     array = new Array();
     array[0] = new Array("1","2","3");
     array[1] = new Array("Name","Age","blabla");
    },

    B: function()
    {
      var c = new this.A();
      alert(c);   //output:: object Object
      alert(c.array[1]); // output:: undefined
      alert(c.array[1][0]); // output undefined
    }    
}

how can i get alerted for example alert(c.array[1][0]) with output "Name". usually in other languages its possible to use methodes from inherited classes but in javascript. i think(hope) it's possible, but how?

Painkiller

1 Answer 1

4

You'd have to change A:

A:function()
{
 this.array = new Array();
 this.array[0] = new Array("1","2","3");
 this.array[1] = new Array("Name","Age","blabla");
},

If you do change it, you'd be better to do this:

A:function()
{
  this.array = [ [ "1", "2", "3" ], [ "Name", "Age", "blabla" ] ];
},

The "Array" constructor is a pretty bad API design and should be avoided.

Sign up to request clarification or add additional context in comments.

1 Comment

Great! Be careful with those array initialization statements - it's the best way to do this, but IE gets upset if you accidentally leave in an extra comma at the end of a list of values.

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.