2012-04-07 73 views
1

我想知道我可以做一些“类似”在javascript(不工作)代码?这可能吗? “Javascript子类”

function Player()  { 

    this.Inventory = function()  { 

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

    } 

} 

,然后用它这样的:

current_player = new Player(); 
current_player.Inventory.UseItem(4); 
+2

值得关注的是,术语“子”通常是指一些在标准的说法一样 - “广告资源”这里真的只是在球员场的对象。 – dfreeman 2012-04-07 01:32:19

+1

事实上,这是组成而不是继承。 – david 2012-04-07 02:28:23

回答

0

function Player()  { 

    var Inventory = { 


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

    }; 

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

尝试。