#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    418
    Rep Power
    20

    Javascript prototype in heritance problem


    Hi,
    I have:
    Code:
    function Game(){
    	this.gameManager = new gameManager();
    }
    Game.prototype.getGameManager = function(){
        return this.gameManager;
    }
    And:
    Code:
    function NetworkGame(){
    }
    NetworkGame.prototype = new Game();
    Now I want to do:
    Code:
    var ng = new NetworkGame()
    var gameManager = ng.getGameManager();
    
    var ng2 = new NetworkGame()
    var gameManager2 = ng.getGameManager();
    However, now gameManager1 and gameManager2 are the same GameManager-object (am i right?), while I want them to have their own copy. What I want is that everytime I do: new NetworkGame(), a new Game() object is created and the NetworkGame inherits all methods from it.

    Is this possible and ifso, is it an uncommon thing to do?

    Hope you can help out. Let me know if I need to give more information.

    Grtz Stefan1
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0
    Yes gameManager1 and gameManager2 are the same GameManager-object . I don't know what you want
    but maybe like this ...

    Code:
    function GameManager(a){this.c=function(){return a}}
    
    Game.prototype=new GameManager;
    Game.prototype.constuctor=Game;
    function Game(a){
    	GameManager.call(this,a);
    }
    
    NetworkGame.prototype = new Game;
    NetworkGame.prototype.constuctor=NetworkGame;
    function NetworkGame(a){
    	Game.call(this,a);
    }
    
    gameManager=new NetworkGame(1)
    gameManager2=new NetworkGame(2)
    
    alert(gameManager.c())
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    418
    Rep Power
    20
    Thanks for your reply.

    I'm not entirely sure how your example works but it doesn't seem like a solution for me. In my real scenario I have multiple properties in the game class and I dont want to change anything to those properties (for example the GameManager should be left unchanged).

    My problem is that I want each NetworkGame to have its prototype set to an entirely new Game object instead of having each NetworkGame use the same Game object as its prototype. Is this possible at all?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    26
    Rep Power
    0
    sorry
    maybe this helps ?...

    Code:
    <script>
    function GameManager(){}
    
    
    function Game(){
    	this.gameManager = new GameManager();;
    }
    Game.prototype.getGameManager = function(){
        return this.gameManager;
    }
    NetworkGame.prototype = new Game();
    NetworkGame.prototype.constructor = NetworkGame;
    function NetworkGame(){
    	Game.call(this);
    }
    
    
    
    var ng = new NetworkGame()
    var gameManager = ng.getGameManager();
    
    var ng2 = new NetworkGame()
    var gameManager2 = ng2.getGameManager();
    
    alert(gameManager2 === gameManager)
    </script>

IMN logo majestic logo threadwatch logo seochat tools logo