0
var Game = function(){

    var game = this;

    game.Player = {
        pts:[]
    }

   }

for example in php it could be as:

game.Computer extends game.Player

But how can I do a child from game.Player in javascript in the same class?

[update] [1]

var Game = function(){

    var game = this;

    game.Player = {
        pts:[],
        test: function(){ alert('test'); }
    }

    Player.prototype = new Computer;

    game.Computer = {

    }
}

var game = new Game();

game.Computer.test();

I'm not sure @BenM

[update] [2]

or even like this:

var Game = function(){

    var game = this;

    game.Player = {
        pts:[],
        test: function(){ alert('test'); }
    }

    game.Player.prototype = game.Computer {

    }

}

var game = new Game();

game.Computer.test();

That is wrong too, can't extend it as is. Sorry if it's a lot of code here.

Smash
  • 513
  • 5
  • 23
  • JavaScript doesn't include extended classes, since it's not truly an OO language. You could do something like this: `Player.prototype = new Computer`. – BenM Mar 16 '13 at 12:34
  • can I then get a Computer like this: `game.Computer`? – Smash Mar 16 '13 at 12:41
  • Assuming you have a `game.Computer` function, yes. – BenM Mar 16 '13 at 12:43
  • I'm updated the source as u said, not working :/ – Smash Mar 16 '13 at 12:50
  • @BenM: It seems the OP rather wants it the other way round. – Bergi Mar 16 '13 at 15:29
  • @AviAtion: Your `Player` object is not even a constructor function, so setting its `prototype` property makes no sense. What do you mean by "making Computer a child of Player" - why don't you make them instances of the same "class" (while [JavaScript has of course no classes](http://stackoverflow.com/a/13418980/1048572))? – Bergi Mar 16 '13 at 15:41

1 Answers1

0

You really seem to be looking for a basic tutorial on how to do inheritance in JavaScript. Here's a good one: http://www.klauskomenda.com/code/javascript-inheritance-by-example/

Alex Weinstein
  • 9,823
  • 9
  • 42
  • 59