2012-04-07 2 views
1

나는 "비슷한"자바 스크립트에서이 (작동하지 않는) 코드를 할 수 있다는 것을 알고 싶습니까? 가능합니까? "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 
     } 
    }; 
} 

가보십시오.

관련 문제