将带参数的方法添加到javascript对象
我目前正在将我的一个java小程序游戏移植到javascript+html5。我以前从未做过面向对象的javascript,这种基于原型的OO东西让我很困惑 我尝试从java实现一个简单的端口,但在做两件事时遇到了困难: 1) 如何在构造函数中运行函数?将带参数的方法添加到javascript对象,javascript,oop,canvas,prototype-programming,Javascript,Oop,Canvas,Prototype Programming,我目前正在将我的一个java小程序游戏移植到javascript+html5。我以前从未做过面向对象的javascript,这种基于原型的OO东西让我很困惑 我尝试从java实现一个简单的端口,但在做两件事时遇到了困难: 1) 如何在构造函数中运行函数? 2) 如何添加具有参数的方法 下面是一些示例代码: function User() { setupStats();// I wanted to put some of the variable initializations into
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...
}
请帮帮我 我推荐道格拉斯·克罗克福德的作品。他清楚地解释了类、私有成员、公共成员、继承等是如何在JavaScript中完成的。你离得不远。问题主要在于你使用了“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();
这将自动将函数引用链接在一起。
我们使用prototype,与所有用户共享setupStats
中的默认设置。我们使用传递上下文,即用户
对象和参数
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...
}
您可能想考虑在类作用域中封装这些方法,如果仍然存在方法歧义,则可以使用点标记来解决命名空间歧义。this.name之所以有效,是因为它是在同一个函数中定义的,但是其他函数不知道它们打算存在于同一个作用域中,因此它们返回未定义的
drawUser()中未定义ctx,因为参数声明不正确。Javascrpit参数应定义为(注意,它们不采用var关键字):
类是使用class关键字声明的[可选,省略方括号]
[private public static] class ClassName [extends ParentClass] { /*methods here*/ }
希望这有帮助。您测试过这个吗?在我看来似乎有很多语法错误。不。我只是想给他一点关于函数原型的想法,以及“this”关键字的范围是如何受到影响的。我最喜欢这种添加方法的方式(因为它使我的移植工作更容易,因为它最让人想起java基于类的OO)。但是,user.init=function()应该是user.prototype.init=function()。答案已接受:)不过,有编辑权限的人可以修复语法错误吗?
[private public static] class ClassName [extends ParentClass] { /*methods here*/ }