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

    Join Date
    Mar 2005
    Posts
    418
    Rep Power
    20

    Javascript inheritance problems


    Hi,
    I have a function called ChessGame() and a function called ChessNetworkGame(). I want to have all objects created by calling 'new ChessNetworkGame()' to inherit the properties from ChessGame() (ie. to make them a child of ChessGame()).

    I always have troubles with inheritance in javascript. I tried the following:
    Code:
    function ChessGame(){
    	this.chessController = new ChessController();
    	// etc.
    }
    
    ChessGame.prototype.isMyTurn = function(){
    	// do something
    }
    
    function ChessNetworkGame(){
    
    }
    ChessNetworkGame.prototype = new ChessGame();
    This gives an error saying: "ChessController() is not defined". However, when I use new ChessGame() anywhere in my script it works fine. What could be the problem here?

    Also is it true that the way I try it above, after doing:
    Code:
    var game = new ChessNetworkGame();
    I should be able to do:
    Code:
    if( game.isMyTurn() ){
        // do something
    }
    Thanks!
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,952
    Rep Power
    4033
    You're probably including the code to define ChessController after your ChessNetworkGame code. As such, when this line executes:
    Code:
    ChessNetworkGame.prototype = new ChessGame();
    Your ChessController object has not yet been defined. Re-order your code to make sure it is defined first.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Posts
    418
    Rep Power
    20
    That's it, thanks!

IMN logo majestic logo threadwatch logo seochat tools logo