0

In my code, I wish to be able to create functions under Game that can be called like so:

Game.Player.iJava.showNum() Which would return '5' and Game.Player.Bill.showNum() Which would return '120'

I'm confused on the syntax to be able to call it like that, this is what I have so far Sorry if my description is vague, I am trying to explain this as best as I can but my English isn't very well

Thank you, here is what I have so far

        var Username = 'iJava';
        function Game(Object, Attribute, Number) {
            this.Object = Object;
            this.Attribute = Attribute;
            this.Number = Number
            this.showNum= function() { return Number;}
            this.Create = function() { if (Object == "Player") { console.log("Created user "+Attribute) } };
        }
        function Player(Name, Num) {
            new Game("Player", Name, Num);
        }
        var User = new Player(Username, 5)
        var Bill = new Player("Bill", 120)
Sam
  • 85
  • 10
  • @JeremyD: No, they are not. – Felix Kling Oct 01 '14 at 22:13
  • @FelixKling Fair enough. I don't understand the question though. iJava is a string, why would you tru to call a function on it. Game doesn't have a Player property either. This is highly confusing. – Jeremy D Oct 01 '14 at 22:13
  • @JeremyD: Notice that the OP calls `new Game("Player", Name, Num);`, and `Game` is defined as `function Game(Object, Attribute, Number) { this.Object = Object; ... }`. So I assume that the *value* of `Object` should actually be used as property name. Of course he'd still have to assign another object to `this[Object]`, not `Object` itself. But I guess that's the core of the problem. – Felix Kling Oct 01 '14 at 22:21

1 Answers1

1

You can put functions as well as anything as properties of other objects:

function Game() { ... }
function Player() { ... }

Game.Player = Player;
Game.Player.Bill = new Game.Player("Bill", 120);
Yogu
  • 9,165
  • 5
  • 37
  • 58
  • Thanks, but how can I create new users like Game.Player.User when User = "Name", and you call it with Game.Player.Name ? – Sam Oct 01 '14 at 22:46