【发布时间】:2012-04-07 01:22:05
【问题描述】:
我想知道我可以在 javascript 中做一些与此(不工作)代码“相似”的事情吗?
function Player () {
this.Inventory = function () {
this.Inventory.UseItem = function(item_id) {
/* use the item ... */
}
}
}
然后像这样使用它:
current_player = new Player();
current_player.Inventory.UseItem(4);
【问题讨论】:
-
值得注意的是,术语“子类”在标准用语中通常意味着其他东西——这里的“库存”实际上只是播放器字段中的一个对象。
-
确实,这是组合而不是继承。
标签: javascript class object subclass