Function arguments inside a function

I want to create some objects, but don't know how to write function arguments inside another function. Here is the code with comments to explain better.

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?

      

Thank.

+3


source to share


1 answer


You want it to types

be its own function, then you can just pass the object to the constructor Troop

.

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 the function definition that was wrong :)

      



In general, I would use class names like types

, and keep the type variables dwarf

in lowercase, but this is more a matter of style than functionality.

+4


source







All Articles