TeAmEr
TeAmEr

Reputation: 4773

Public array is not accessable in js class

here's a part of the class :

function Table(seats){
    //editables
        var leaveTable_position=new Array('380','0','102','20');

    //runtime
        Table.id=0;
        Table.max_buy=0;
        Table.min_buy=0;
        Table.player_timeout=0;

    //on creation
        Table.seat=new Array();
        if (seats<5){seats=5;}
        if (seats>5){seats=9;}
        for (var i=1 ; i<=seats ; i++){
            Table.seat[i]=new Seat(i);
            Table.seat[i].create();
        }}

you see the Table.seat public array ? assuming i have 3 seats (table.seat[0]; table.seat[2];) ...

the following code gives me 'seat is undefined' !!!

table=new Table();
table.seat[2].getUser();

any ideas why ? am not that good in js oop !

Upvotes: 1

Views: 284

Answers (2)

Rob W
Rob W

Reputation: 349032

You have to use this instead of Table. When using Table, you're modifying properties on the Table function.

If you use this, the properties are defined on the current instance of the Table "class". If you still want to prefix Table, declare var Table = this inside your function. A side-effect of this is that you cannot directly call Table() from inside the function any more.

function Table(seats){
        var Table = this;
    //editables
        var leaveTable_position=new Array('380','0','102','20');

    //runtime
        Table.id=0;
        Table.max_buy=0;
        Table.min_buy=0;
        Table.player_timeout=0;

    //on creation
        Table.seat=new Array();
        if (seats<5){seats=5;}
        if (seats>5){seats=9;}
        for (var i=1 ; i<=seats ; i++){
            Table.seat[i]=new Seat(i);
            Table.seat[i].create();
        }}

Upvotes: 4

Naftali
Naftali

Reputation: 146310

Do not use Table use this.

For example:

//runtime
    this.id=0;
    this.max_buy=0;
    this.min_buy=0;
    this.player_timeout=0;

See fiddle: http://jsfiddle.net/maniator/a7H57/3/

Upvotes: 3

Related Questions