April 7, 2012

Is it possible ? "Javascript Subclass"

Question by EyeSalut

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

Answer by Starx

Yeah

function Player ()     {

    var Inventory = {


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

    };

}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!