在本教程中,我創建了一個將繼承Actor超類的宇宙飛船。當創建飛船的構造函數,我調用使用Actor.call(此,場景,X,Y)的演員構造調用超類構造函數的HTML5遊戲
誰能告訴我Actor.call(此,場景,X,Y)的目的我認爲這是一種減少重複的方法,因爲我能夠繼承Actor的屬性(this.scene = scene,this.x = x和this.y = y)?
function Actor(scene, x, y) {
this.scene = scene;
this.x = x;
this.y = y;
scene.register(this);
}
Actor.prototype.moveTo = function(x, y) {
this.x = x;
this.y = y;
this.scene.draw();
};
Actor.prototype.draw = function() {
var image = this.scene.images[this.type];
this.scene.context.drawImage(image, this.x, this.y);
};
function SpaceShip(scene, x, y) {
**Actor.call(this, scene, x, y); // call actor constructor**
this.points = 0;
}
SpaceShip.prototype = Object.create(Actor.prototype)
SpaceShip.prototype.type = "spaceShip";
SpaceShip.prototype.scorePoint = function() {
this.points++;
};
你的問題只是「什麼是構造函數」? – meagar
我的問題是 - 在Spaceship構造函數中您有任何Java(或任何類似的面向對象的語言)體驗的Actor.call(this,scene,x,y) – stckpete
的用途是什麼?另外,你知道原型繼承如何在JavaScript中工作嗎? – valepu