1

I want to know i can do something "similar" to this (not working) code in javascript ?

function Player ()     {

    this.Inventory = function ()     {

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

    }

}

and then use it like that :

current_player = new Player();
current_player.Inventory.UseItem(4);
2
  • 2
    Worth noting that the term "subclass" usually means something else in standard parlance -- 'Inventory' here is really just an object in a field on Player. Commented Apr 7, 2012 at 1:32
  • 1
    Indeed, this is composition rather than inheritance. Commented Apr 7, 2012 at 2:28

3 Answers 3

3
function Player() {
    this.Inventory = {
        UseItem: function(item_id) {
            // code here
        }
    };
}

Try that.

Sign up to request clarification or add additional context in comments.

Comments

0
current_player = new Player();
current_player.prototype.Inventory = {
         UseItem : function(item_id)    {
            /* use the item ... */
        }
}

Comments

-1

Yeah

function Player ()     {

    var Inventory = {


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

    };

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.