0
var Game = function(){

    var game = this;

    game.Player = {
        pts:[]
    }

   }

例如在 php 中它可能是:

game.Computer extends game.Player

但是我怎样才能在同一个班级的 javascript 中从 game.Player 做一个孩子?

[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();

我不确定@BenM

[update] [2]

甚至像这样:

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();

那也是错误的,不能按原样扩展它。对不起,如果这里有很多代码。

4

1 回答 1

0

您似乎真的在寻找有关如何在 JavaScript 中进行继承的基本教程。这是一个很好的:http ://www.klauskomenda.com/code/javascript-inheritance-by-example/

于 2013-03-17T07:36:06.940 回答