2012-04-07 11 views
1

私は、この(動作していない)コードとjavascriptで「似たような」ことができることを知りたいですか?可能ですか? "Javascript Subclass"

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

+1

実際、これは継承ではなく構成です。 – david

答えて

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

はそれを試してみてください。

関連する問題