可能吗&引用;Javascript子类;

可能吗&引用;Javascript子类;,javascript,class,object,subclass,Javascript,Class,Object,Subclass,我想知道我可以在javascript中做一些与此(不工作)代码“类似”的事情吗 function Player () { this.Inventory = function () { this.Inventory.UseItem = function(item_id) { /* use the item ... */ } } } 然后像这样使用它: current_player = new P

我想知道我可以在javascript中做一些与此(不工作)代码“类似”的事情吗

function Player ()     {

    this.Inventory = function ()     {

        this.Inventory.UseItem = function(item_id)    {
            /* use the item ... */
        }

    }

}
然后像这样使用它:

current_player = new Player();
current_player.Inventory.UseItem(4);
试试看。

是的

function Player ()     {

    var Inventory = {


        UseItem : function(item_id)    {
            /* use the item ... */
        }

    };

}

值得注意的是,术语“子类”在标准术语中通常意味着其他东西--“库存”在这里实际上只是玩家场中的一个对象。事实上,这是组合而不是继承。
function Player() {
    this.Inventory = {
        UseItem: function(item_id) {
            // code here
        }
    };
}
function Player ()     {

    var Inventory = {


        UseItem : function(item_id)    {
            /* use the item ... */
        }

    };

}