-2

While I am writing a constructors' methods like the "runGame" method of the "Game" constructor, if I need to reference a property of the "GameBoard" constructor should I use the name of the constructor, like this:

function Game(){
   this.runGame(){
     var someProp = GameBoard.otherProp;
   }
}

or do I have to create an instance of the constructor object first and then refer to the instance like this.

var newGameBoard = new GameBoard();

function Game(){
   this.runGame(){
     var someProp = newGameBoard.otherProp;
   }
}
Drazah
  • 37
  • 1
  • 7
  • We can't answer the question, because your "to" form is invalid, you have a syntax error as of the `{` after `this.runGame()` inside `Game`. It matters, because if we don't know how your objects are *really* organized, we can't tell you how to correctly deal with them. – T.J. Crowder Jul 17 '17 at 15:13
  • There should be *very little* code in the constructors - perhaps, creation/assignment of the relevant objects that are intrinsically available. Most work (including access to other objects, as required) happens in the methods. – user2864740 Jul 17 '17 at 15:16
  • The "to" isn't part of the code. I was trying to show that the code was being converted from one format to another. I probably should have just left that whole first portion out. I will edit the question. – Drazah Jul 17 '17 at 15:21
  • @Drazah: The code in the question remains a syntax error. It's great people have tried to help (though I would argue they should have waited until the question was clear), but really, code without syntax errors so we know what you're doing is the starting point. – T.J. Crowder Jul 17 '17 at 15:29

3 Answers3

1

If I've understood your question in the right way, what you need is composition and you need to inject associated instances during construction time:

function Game(gameBoard) {
   this.gameBoard = gameBoard;
}

Game.prototype = {
    runGame: function() {
        // You access injected GameBoard through the 
        // own Game object's property "this.gameBoard"
        var someProperty = this.gameBoard.someProperty;
    }
};

var gameBoard = new GameBoard();
var game = new Game(gameBoard);

Further reading:

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • If you're going to replace the object referenced by the `prototype` property, be sure to set `constructor` correctly. – T.J. Crowder Jul 17 '17 at 15:19
  • Yes ! Faster, shorter and better than my answer. @T.J.Crowder I didn't understand your comment, do you mean we should define a constructor inside the prototype object ? I never did that, never had any problem but please enlighten me – Mouradif Jul 17 '17 at 15:22
  • @T.J.Crowder I understand that this is a notice for the OP, isn't it? – Matías Fidemraizer Jul 17 '17 at 15:23
  • @MatíasFidemraizer: Well, not really. If you're going to have an example advocating doing something they're not doing (replacing the `prototype` property), it's important to show doing it correctly. – T.J. Crowder Jul 17 '17 at 15:28
  • 1
    @KiJéy: See: https://stackoverflow.com/questions/8453887/why-is-it-necessary-to-set-the-prototype-constructor – T.J. Crowder Jul 17 '17 at 15:31
  • @MatíasFidemraizer - Thanks! I am new to writing object oriented javascript and your example seems like a much better approach then what I was doing. Those Wikipedia links give a great explanation of these techniques. Thanks! – Drazah Jul 17 '17 at 16:31
  • @T.J.Crowder Thanks ! Good to know ! – Mouradif Jul 17 '17 at 21:31
1

If every Game has a GameBoard, it should be a property:

function Game(){
  this.board=new Board();
}

Game.prototype.runGame=function(){//real inheritance
  var someProp = this.board.otherProp;
};
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

If the GameBoard(s) belong to the Game in your logic, here's how I'd do it

var Game = function(params) {
    this.options = params.options; // it could prove useful to instanciate a game using a set of rules
    this.gameBoards = params.gameBoards; // Already instanciated gameBoard(s)
    this.activeGameBoard = null; // if there are many gameboards it might be a good idea to keep track of the one that's currently active
    this.prop = '';
    // ... Initialize all the properties you need for your Game object
}

Game.prototype = {
    runGame: function(gameBoardIndex) {
        this.activeGameBoard = this.gameBoards[index];
        this.someProp = this.activeGameBoard.someProp;
    }
}

I know I'm assuming a lot of things but I can't help it, it reminds me the only project I worked on that involved games and gameboards :p

Mouradif
  • 2,666
  • 1
  • 20
  • 37