Javascript 函数中函数的参数
我想创建一些对象,但不知道如何在另一个函数中写入函数的参数。下面是带注释的代码,以便更好地解释Javascript 函数中函数的参数,javascript,jquery,Javascript,Jquery,我想创建一些对象,但不知道如何在另一个函数中写入函数的参数。下面是带注释的代码,以便更好地解释 function Troop(rss, time, offense, defense){ this.rss= rss; this.time= time; this.offense= offense; this.defense= function types(a, b, c, d){ this.a= a; this.b= b; this.c= c;
function Troop(rss, time, offense, defense){
this.rss= rss;
this.time= time;
this.offense= offense;
this.defense= function types(a, b, c, d){
this.a= a;
this.b= b;
this.c= c;
this.d= d;
}
}
Dwarf = new Troop(1,2,3, new types(11,22,33,44)); // this most be wrong
alert(Dwarf.defense.a) // how can I access to the values after?
谢谢。如果希望
类型
成为它自己的函数,那么只需将对象传递给队伍
构造函数即可
function types(a, b, c, d) {
this.a= a;
this.b= b;
this.c= c;
this.d= d;
}
function Troop(rss, time, offense, defense){
this.rss= rss;
this.time= time;
this.offense= offense;
this.defense= defense;
}
Dwarf = new Troop(1,2,3, new types(11,22,33,44)); // these lines are right
alert(Dwarf.defense.a) // it's the function definition that was wrong :)
一般来说,我会大写类名称,如
类型
,并保留变量,如矮化
小写,但这更多的是风格问题,而不是功能问题。我考虑过,但最后两行是对的?他们会工作吗?是的,他们工作得和你一样好。我忍不住改变了类型的大小写,但我唯一真正做的改变是将类型
分解成一个单独的函数。不要在部队中定义它。我只是恢复了我的风格建议,以免把问题搞得一团糟。