2014-01-07 45 views
0

我正在學習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/

+1

你能解釋一下你想達到什麼樣的? – diolemo

+0

它沒有多大意義,我認爲你在混合東西...... – elclanrs

+0

可能的重複[JavaScript中的多重繼承/原型](http://stackoverflow.com/questions/9163341/multiple-inheritance-prototypes- in-javascript) –

回答

1

因爲球員可以控制,你可以在控制混合使用播放器。你的對象構造函數是一個嚴重選擇的名字,因爲構造函數應該以一個使它爲Object的大寫字母開頭,並且你會覆蓋window.Object(壞主意)。出於這個原因,我將它重命名爲Base。球員是一個基礎對象和可以控制所以繼承從基地和控制混合在

有關構造函數的更多信息,混合ins,實例成員和原型檢查this link

function Base() { 
    this.speed = 1; 
    this.walkDistant = 5; 
} 

// sub Object2 
function Controls() { 
} 
Controls.prototype.moveLeft = function() { 
    console.log(this.speed, this.walkDistant); 
    return this.speed * this.walkDistant; 
} 

// super Object 
function Player() { 
    //make player have Base instance members 
    Base.call(this); 
    //make player heve Controls instance members 
    Controls.call(this); 
} 
//player is a base object 
Player.prototype = Object.create(Base.prototype); 
//repair constrictor 
Player.prototype.constructor = Player; 
//Player can be controlled, copy controls prototype on player (mixin) 
// this would be better suited in a helper function, see link posted in answer 
var stuff; 
for (stuff in Controls.prototype) { 
    if (Controls.prototype.hasOwnProperty(stuff)) { 
    Player.prototype[stuff] = Controls.prototype[stuff]; 
    } 
} 

var firstPlayer = new Player(); 

console.log(firstPlayer.moveLeft()); 

如果你想讓玩家已經控制你可以嘗試這樣的事:

function Controls(what) { 
    //what do we need to control 
    this.controlWhat=what; 
} 
Controls.prototype.moveLeft = function() { 
    console.log(this.controlWhat.speed, this.controlWhat.walkDistant); 
    return this.controlWhat.speed * this.controlWhat.walkDistant; 
}; 

function Player() { 
    this.speed = 1; 
    this.walkDistant = 5; 
    this.controls=new Controls(this); 
} 
var firstPlayer = new Player(); 

console.log(firstPlayer.controls.moveLeft()); 
+0

非常感謝您的答案,介意解釋你爲什麼做'Player.prototype.constructor = Player;'?爲什麼不使用new而不是'Player.prototype = Object.create(Base.prototype); //修蟒'? –

+0

我發佈的鏈接應該回答這兩個問題 – HMR

+0

是的,非常感謝,似乎事情並不完全按照我的想法工作 –

0

您可以使用Mixins擴展的其他對象已經實現的對象使用功能的功能。你也可以讓子對象知道超級對象如下。

function subClassA(containerClass) { 
    this.containerClass = containerClass; 
    this.methodA = function() { 
     console.log(this.containerClass.b.methodB()); 
    } 
} 

function subClassB(containerClass) { 
    this.containerClass = containerClass; 
    this.methodB = function() { 
     return 12345; 
    } 
} 

function containerClass() { 
    this.a = new subClassA(this); 
    this.b = new subClassB(this); 
} 

var cc = new containerClass(); 
cc.a.methodA(); 

的密新方法將是這個樣子:

// extend function to add mixin support 
function extend(destination, source) { 
    for (var k in source) 
     if (source.hasOwnProperty(k)) 
     destination[k] = source[k]; 
    return destination; 
} 

function subClassA() { } 
subClassA.prototype.methodA = function() { 
    console.log(this.methodB()); 
}; 

function subClassB() { } 
subClassB.prototype.methodB = function() { 
    return 12345; 
}; 

function superClass() { 
    // ---------------- 
} 

// add the subClassA and subClassB functionality 
extend(superClass.prototype, subClassA.prototype); 
extend(superClass.prototype, subClassB.prototype); 

var sc = new superClass(); 
sc.methodA(); 
+0

是啊我做了這樣的事情之前,我確信有一種方法可以做到這一點與原型我的意思是這些對象是在同一個父母之下的,他們怎麼不知道彼此? –

+0

@NetaMeta不應該彼此瞭解。它只是不能這樣工作。 – diolemo

+0

@NetaMeta我已經更新了包含使用Mixins的解決方案的答案。看看這是否更適合你。 – diolemo

0

的問題是,您試圖訪問從subObj2 pertences到subObj1的屬性,而是繼承兩個superObj

要做到這一點,您應該讓您的subObj1繼承subObj2

// sub Object1 
function name(){ 
    this.name = function(){ 
     var myName = 'FirstName'; 
     console.log(myName, this.last.lastName); 
    } 

    this.callName = function(){ 
     this.name(); 
    }; 
} 

// sub Object2 
function lastName(){ 
    this.lastName ='someLastName'; 
} 

// super Object 
function fullName(){ 
    // DoesNothing 
} 

name.prototype.last = new lastName(); 
fullName.prototype.name = new name(); 

var myName = new fullName(); 
myName.name.callName(); 

你可以看到這個fiddle

+0

這會將lastName()功能添加到所有name()對象。如果我理解正確,我不認爲這是想要的。 – diolemo

+0

我不想讓子對象1和子對象2相關,它們是2個獨立的對象,它們唯一的關係是通過超級對象。 –

+0

@NetaMeta子對象可以從父母訪問屬性,但不能從其他孩子訪問屬性。你應該將你的'moveLeft'方法移動到你的超類,以實現你想要的(但這也沒有意義)。我認爲我們有一個與面向對象概念相關的問題,而不是特定於JS的問題。 – Beterraba

相關問題