javascript 将带参数的方法添加到javascript对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7494469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add method with parameters to javascript object
提问by Razor Storm
I am currently in process of porting one of my java applet games to javascript+html5. I have never done object oriented javascript before and this prototype based OO stuff is confusing me a lot.
我目前正在将我的一款 Java 小程序游戏移植到 javascript+html5。我以前从未做过面向对象的 javascript,这个基于原型的面向对象的东西让我很困惑。
I tried to do a straightforward port from java but am having trouble doing two things:
我试图从 java 做一个简单的端口,但在做两件事时遇到了麻烦:
1) How do I run a function inside a constructor?
2) How do I add a method that has a parameter?
1) 如何在构造函数中运行函数?
2)如何添加一个有参数的方法?
Heres some example code:
下面是一些示例代码:
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...
}
Please help guys!
请帮助伙计们!
回答by Joe
We are using prototype, to share the defaults in setupStats
with all Users
. We are using callto pass a context, being the User
object, and a parameter
;
我们正在使用原型,setupStats
与所有Users
. 我们使用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...
}
回答by fdfrye
You aren't too far off. The trouble is mostly your use of the 'this' keyword.
你离得不远了。问题主要在于您使用了“this”关键字。
You want something more like:
你想要更像:
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);};
};
You would continue this approach and execute calls against it by doing things like
您将继续这种方法并通过执行以下操作来执行针对它的调用
user.init();
which would automatically chain your function references together.
这会自动将您的函数引用链接在一起。
回答by awm
I recommend reading JavaScript: The World's Most Misunderstood Programming Languageby Douglas Crockford. He explains clearly how classes, private members, public members, inheritance, etc. are done in JavaScript.
我推荐阅读Douglas Crockford所著的 JavaScript:世界上最容易被误解的编程语言。他清楚地解释了类、私有成员、公共成员、继承等在 JavaScript 中是如何完成的。
回答by awiebe
You may want to consider encasing these methods in class scope, if there is still method ambiguity you can use dot notation to resolve the namespace ambiguity. this.name works because it is defined in the same function, however other functions do not know that they are intended to exist in the same scope, thus they return undefined.
您可能需要考虑将这些方法封装在类范围内,如果仍然存在方法歧义,您可以使用点表示法来解决命名空间歧义。this.name 之所以有效,是因为它定义在同一个函数中,但是其他函数不知道它们打算存在于同一范围内,因此它们返回 undefined。
ctx is not defined in drawUser() because the parameters are declared incorrectly. Javascrpit params should be delared as (NB they do not take the var keyword):
ctx 未在 drawUser() 中定义,因为参数声明不正确。Javascrpit 参数应声明为(注意它们不采用 var 关键字):
function methodName( aParam : aParamType, bParam : bParamType) {}
classes are declared using the class keyword [optional, omit square brackets]
类是使用 class 关键字声明的 [可选,省略方括号]
[private public static] class ClassName [extends ParentClass] { /*methods here*/ }
hope this helps.
希望这可以帮助。