2011-09-21 106 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*/ } 

希望这会有所帮助。