Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 函数中函数的参数_Javascript_Jquery - Fatal编程技术网

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 :)

一般来说,我会大写类名称,如
类型
,并保留变量,如
矮化
小写,但这更多的是风格问题,而不是功能问题。

我考虑过,但最后两行是对的?他们会工作吗?是的,他们工作得和你一样好。我忍不住改变了类型的大小写,但我唯一真正做的改变是将
类型
分解成一个单独的函数。不要在
部队中定义它。我只是恢复了我的风格建议,以免把问题搞得一团糟。