2

Let's say you're making a game. You want to try and not pollute the global scope and possibly limit the user's ability to easily alter the game (doubtful with client-side). You feel like modules might be unnecessary for your purposes. Is it bad practice to pass references to a class to another class during instantiation to access its methods?

Contrived example:

//game.js

var Game = (function () {
    function Game() {
        this.currentLevel = null;
        this.score = 0;
    }

    Game.prototype.addScore = function (num) {
        this.score += num;
    };

    Game.prototype.goToLevel = function (diff) {
        this.currentLevel = new Level(this, diff);
    };

    Game.prototype.returnHome = function (level) {
        this.currentLevel = null;
    };

    return Game;
})();

//level.js

var Level = (function () {
    function Level(game, difficulty) {
        this.game = game; //reference to game
        this.difficulty = difficulty;
        this.entities = [];
        this.load();
    }

    Level.prototype.load = function () {
        this.addEntity({name: 'tim', power: 23, difficulty: this.difficulty});
    };

    Level.prototype.leave = function () {
        this.game.returnHome();
    };

    Level.prototype.addEntity = function (options) {
        this.entities.push(new Entity(this, options));
    };

    Level.prototype.removeEntity = function (entity) {
        for(var x = 0; x < this.entities.length; x++) {
            if(this.entities[x] === entity) this.entities.splice(x, 1);
        }
    };

    return Level;
})();

//level.js

var Entity = (function () {
    function Entity(level, options) {
        this.level = level; //reference to level
        this.options = options;
    }

    Entity.prototype.kill = function () {
        this.level.removeEntity(this); // anti-pattern?
        this.level.game.addScore(34.53); // too closely coupled?
    };

    return Entity;
})();

//main.js

var Main;
(function (Main) {
    var game = null;

    function documentIsReady() {
        start(); // Start the game
    }

    function start() {
        game = new Game();
        game.goToLevel('hard');
    }

    return {
        documentIsReady: documentIsReady
    }
})(Main || (Main = {}));

$(document).ready(function () {
    Main.documentIsReady();
});

Forgive the half-baked example. If you end up with many instances of the 'Entity' class, do all the references to 'Level', though the same instance, start taking more memory? Are there other pitfalls? Another method would be to implement some kind of interface that you can access that allow classes to talk to each other.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
loubeasley
  • 49
  • 6
  • In a general sense there's nothing wrong with object A having a property that refers to object B, and then having object A call B.someMethod(). – nnnnnn Feb 13 '16 at 01:27
  • Thanks for your response. If you had 1000 instances of Entity that each had references to level, is there any kind of memory impact? – loubeasley Feb 13 '16 at 01:33
  • I wouldn't expect 1000 instances referring to the same parent object to be a memory problem. It can feel a little clumsy for child objects to have to maintain a reference to their parent - it generally feels more natural for the parent to maintain a list of children - but sometimes you just have to do that. I don't have a better pattern for the type of thing you are implementing, but perhaps somebody else will suggest something. – nnnnnn Feb 13 '16 at 01:53
  • Yeah the actual implementation I have loops through the children in most cases to perform methods like .update() and .draw() etc but there are a few cases where I need to get or alter some data held in the parent from the child and I just wondered what the general consensus on that sort of thing was. Thank you for your thoughts – loubeasley Feb 13 '16 at 02:18
  • Possible duplicate of [How to use mixins properly in Javascript](http://stackoverflow.com/questions/13632298/how-to-use-mixins-properly-in-javascript) – Paul Sweatte Nov 22 '16 at 16:38

0 Answers0