2012-04-07 68 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 
     } 
    }; 
} 

嘗試。