1

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 !

1
  • 3
    You might want to read this MSDN tutorial on OOP programming in javascript: developer.mozilla.org/en/…. In particular, that javascript is Prototype-based programming in which "is a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is accomplished through a process of decorating existing objects which serve as prototypes" Commented Nov 4, 2011 at 17:12

2 Answers 2

4

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

Comments

3

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/

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.