我正在學習js的繼承和原型,我的命名可能完全關閉,我很抱歉。我如何繼承2個對象,並使用這兩個對象作爲另一個原型
我想創建一個超級對象和原型2子對象作爲屬性,然後在其中一個子對象調用另一箇中找到的函數。它由於某種原因不起作用。
UPDATE
我在這裏的目標是: 我試圖做一個小遊戲 - 樂趣和實踐。 我的計劃是有一個名爲(對象)的基本對象,它具有定位和其他屬性(每個其他對象都有),另一個對象稱爲(controls)控件。只有可以移動的對象也會擁有該對象。
玩家也是對象,他們將同時擁有「對象」和「控制」。作爲他們的原型。
希望清理一下東西。
代碼:
// sub Object1
function object(){
this.speed = 1;
this.walkDistant = 5;
}
// sub Object2
function controls(){
this.moveLeft = function(){
console.log(this.speed , this.walkDistant);
return this.speed * this.walkDistant;
}
}
// super Object
function player(){
// DoesNothing
}
player.prototype.object = new object();
player.prototype.controls = new controls();
var firstPlayer = new player();
console.log(firstPlayer.controls.moveLeft());
或者如果你喜歡小提琴:http://jsfiddle.net/rMaKa/1/
你能解釋一下你想達到什麼樣的? – diolemo
它沒有多大意義,我認爲你在混合東西...... – elclanrs
可能的重複[JavaScript中的多重繼承/原型](http://stackoverflow.com/questions/9163341/multiple-inheritance-prototypes- in-javascript) –