2011-09-21 69 views
2

我目前正在將我的一個java applet遊戲移植到javascript + html5。我從來沒有做過面向對象的JavaScript,而這個基於OO的原型讓我很困惑。將參數添加到javascript對象

我試着做從Java一個簡單的端口,但我有麻煩做兩件事情:

1)如何運行一個構造函數中的功能?
2)如何添加一個有參數的方法?

繼承人一些示例代碼:

function User() 
{ 
    setupStats();// I wanted to put some of the variable initializations into 
    // a separate function for code modularity reasons. 

    this.name='bob'; 

    //However that doesn't seem to work 
    alert(this.gold); // gets Undefined 

    alert(this.name); // gets bob. Phew at least this works 

    //I also want to add a method with a parameter in it: 
    this.draw=function(ctx){drawUser(ctx);}; 
} 

function setupStats() 
{ 
    this.gold=2; 
    this.exp=3; 
    this.blah='blah'; 
    this.that='something else'; 
    this.superultraomg='insert some computation'; 
} 

function drawUser(ctx) 
{ 
    ctx.drawImage(blah,blah,blah); 
    alert(ctx); // Also gets undefined. Uh oh... 

    alert(this.name); //Undefined? WHAT IS THIS I DONT EVEN... 
} 

請幫傢伙!

回答

3

你不是太遙遠。麻煩主要是你使用'this'關鍵字。

你想要更多的東西一樣:

var user = {}; 

    var user.setupStats = function() 
    { 
     this.gold=2; 
     this.exp=3; 
     this.blah='blah'; 
     this.that='something else'; 
     this.superultraomg='insert some computation'; 
    }; 

    var user.init = function() 
    { 
     this.name='bob'; 

     //Setup the stats 
     this.setupStats(); 

     //However that doesn't seem to work 
     alert(this.gold); // gets Undefined 

     alert(this.name); // gets bob. Phew at least this works 

     //I also want to add a method with a parameter in it: 
     this.draw=function(ctx){drawUser(ctx);}; 
    }; 

你會繼續這一做法,並通過做這樣的事情

user.init(); 

這將自動鏈中的函數引用一起執行反對電話。

+0

你測試過了嗎?看起來像一些語法錯誤給我。 – awm

+0

沒有。我只是想給他一些關於函數原型的想法,以及'this'關鍵字的範圍如何受到影響。隨意推薦編輯以使代碼更加複製/粘貼友好。 – fdfrye

+0

我最喜歡這種添加方法的方式(因爲它讓我的移植工作更容易,因爲它最容易讓人聯想到基於Java的基於類的OO)。但是,您的user.init = function()應該是user.prototype.init = function()。答案接受:)具有編輯權限的人可以修復語法錯誤嗎? –

3

Example

我們正在使用的原型,與所有Users共享默認的setupStats。我們使用call來傳遞上下文,即User對象和parameter;

function User() 
{ 
    setupStats();// I wanted to put some of the variable initializations into 
    // a separate function for code modularity reasons. 

    this.name='bob'; 

    //However that doesn't seem to work 
    alert(this.gold); // gets Undefined 

    alert(this.name); // gets bob. Phew at least this works 

    //I also want to add a method with a parameter in it: 
    this.draw= function(ctx){ drawUser.call(this, ctx); }; 
} 

function setupStats() 
{ 
    this.gold=2; 
    this.exp=3; 
    this.blah='blah'; 
    this.that='something else'; 
    this.superultraomg='insert some computation'; 
} 

User.prototype = new setupStats(); 

new User().draw('pinky'); 

function drawUser(ctx) 
{ 
    //ctx.drawImage(blah,blah,blah); 
    alert(ctx); // Also gets undefined. Uh oh... 

    alert(this.name); //Undefined? WHAT IS THIS I DONT EVEN... 
} 
+0

+1。這工作很好 –

0

你可能要考慮包裹類範圍這些方法,如果還有不確定性的方法,你可以使用點表示法來解決命名空間的模糊性。 this.name可以工作,因爲它是在同一個函數中定義的,但其他函數並不知道它們打算存在於同一個作用域中,因此它們返回undefined。

ctx未在drawUser()中定義,因爲參數聲明不正確。 JAVASCRPIT PARAMS應該delared爲(NB他們不走var關鍵字):

function methodName(aParam : aParamType, bParam : bParamType) {} 

類是使用class關鍵字聲明[可選,省略方括號]

[private public static] class ClassName [extends ParentClass] { /*methods here*/ } 

希望這會有所幫助。