0

This is basically two errors but they go hand in hand since the second error goes back to the first one

Uncaught TypeError: Cannot set property 'Players' of undefined

Uncaught TypeError: Cannot read property 'CreatePlayer' of undefined

var TotalPlayers = 0;
Player.Players = [];

Player.CreatePlayer = function (name, Class, rank) {
    TotalPlayers ++;
    Player.Players[TotalPlayers] = {
        name: name,
        Class: Class,
        rank: rank
    };
    Player[name] = new Player(name, Class, rank);
};

There is more to the code than this but this should be the only parts needed

2 Answers2

1

Where are you declaring what Player is? With the code you're presenting, you're missing its initialization:

var Player = {
    Players: []
};
var TotalPlayers = 0;

I just read the code you posted, Player is defined after using it for the first time, so at the very least you should move the following to the top:

var Player = function(name, Class) {
    // ...
};

Another option would be to declare it using the function statement:

function Player(name, Class) { ... };

This will give you the same result and since in javascript functions are evaluated first, the name will always be available. You may benefit from reading more about Function Declarations vs. Function Expressions.

Jaime Gómez
  • 6,961
  • 3
  • 40
  • 41
1

Try to add a var Player = {} after (or before) var TotalPlayers = 0;

in addition instead of counting the total players you can simply push them to the array:

Player.Players.push({
        name: name,
        Class: Class,
        rank: rank
    });
Simon
  • 240
  • 1
  • 6