Jump to content

Passing object into class as argument


Chimera
 Share

Recommended Posts

So I am fairly new to OOP, especially with javascript. I have created some pretty basic classes and been able to successfully instantiate some objects from those classes by passing in strings, ints, and arrays as arguments, but I seem to be stumbling with how to pass another object into the class.

 

I provided a simple example of what I am trying to do, the console does not display any errors, but it reads the object passed in as 'undefined'. If I access the object outside of the class I am passing it into, I get a full readout on the properties within the class.

 

I have a gut feeling that I may have to use constructor .prototype method in the class, but I couldn't find any great examples targeting objects as the argument. 

          function Hero(NAME, LVL, CLASS, STATS, WEAPON, ARMOUR, BOOTS) {            this.name = NAME; //string            this.level = LVL; //int            this.class = CLASS; //object            this.vitality = STATS[0]; //array            this.strength = STATS[1]; //array             this.dexterity = STATS[2]; //array             this.speed = STATS[3]; //array             this.intelligence = STATS[4]; //array             this.mentality = STATS[5]; //array             this.weapon = WEAPON; //object            this.armour = ARMOUR; //object            this.boots = BOOTS; //object          }          var player = new Hero          (            'King',            1,            Berserker,            [9,11,8,8,7,7],            LargeAxe,            LeatherChest,            LeatherBoots          );

If anyone has an explanation, link, or example I would appreciate the help.

Link to comment
Share on other sites

Not necessarily your issue, but something to be wary of, "class" is a reserved word in JavaScript [ See: http://www.w3schools.com/js/js_reserved.asp ] - I believe you shouldn't use it as a name for your variables.

Presuming that Berserker, LargeAxe, etc. are the objects you are having problems with, perhaps you could include where they are defined? Also can you clarify, are you trying to pass an instance of the object or in the case of a function with methods and properties, do you want to pass that function as an argument?

Link to comment
Share on other sites

Here are the classes for some of the other object, even simpler than the above example. Thank you for the tip on the reserved word, seems like something I should have known, I will change that shortly. 

 

If there is another way to accomplish the goal of getting some of the object data into the player object, then I all for giving it a try. Right now I am kind of playing around with the different classes so I don't have a definitive design on what I need. I will need a way to relate the player with the "class", and other item objects. I mainly need the properties for the items, and the "class" might have some skills I might implement later which will may be methods.

 

I just tried to change the class name to something else and print it out in the console, same result, undefined. When I print out the object directly, I get the below print out.

Class {name: "Berserker", base: Array[6], apt: Array[6]}apt: Array[6]base: Array[6]name: "Berserker"__proto__: Class
/***********************          PLAYER CLASS          ***********************/          function Hero(NAME, LVL, BER, STATS, WEAPON, ARMOUR, BOOTS) {            this.name = NAME;            this.level = LVL;            this.ber = BER;            this.vitality = STATS[0];            this.strength = STATS[1];            this.dexterity = STATS[2];            this.speed = STATS[3];            this.intelligence = STATS[4];            this.mentality = STATS[5];            this.weapon = WEAPON;            this.armour = ARMOUR;            this.boots = BOOTS;            console.log(this.ber);            var attack = function(enemy) {            }          }          var player = new Hero          (            'King',            1,            Berserker,            [9,11,8,8,7,7],            LargeAxe,            LeatherChest,            LeatherBoots          );          /***********************          ENEMY CLASS          ***********************/          function Enemy(NAME, LVL, CLASS, STATS, WEAPON, ARMOUR, BOOTS) {            this.name = NAME;            this.level = LVL;            this.class = CLASS;            this.vitality = STATS[0];            this.strength = STATS[1];            this.dexterity = STATS[2];            this.speed = STATS[3];            this.intelligence = STATS[4];            this.mentality = STATS[5];          }          var enemy = new Enemy          (            'Manning',            1,            Berserker,            [8,12,7,9,5,9],            LargeAxe,            LeatherChest,            LeatherBoots          );          /***********************          CLASSES CLASS          ***********************/          function Class(NAME, BASE, APT) {            this.name = NAME; // Class name            this.base = BASE; // Base stats for starting class, VIT, STR, DEX, SPD, INT, MENT            this.apt = APT; // Aptitude for stat leveling, S = 110, A = 100, B = 90, C = 80, D = 70, E = 60            //this.skills = SKILLS; // Class Skills, array          }          var Berserker = new Class          (            'Berserker',             [10,10,10,10,10,10],             [90,110,80,80,70,70]          );          console.log(Berserker);          console.log(Berserker.base);          console.log(Berserker.apt);          /***********************          WEAPONS CLASS          ***********************/          function Weapon(NAME, TYPE, CLASS, DAM, SIZE, WEIGHT, BLOCK, STATS, EFFECT) {            this.name = NAME; // Weapon name            this.type = TYPE; // Weapon type, sword, spear, staff, etc            this.class = CLASS; // Class restrictions, array            this.damage = DAM; // Damage            this.size = SIZE; // Size, 1h or 2h            this.weight = WEIGHT; // Weight            this.block = BLOCK; // Block percentage            this.stats = STATS; // Bonus stats, array(VIT,STR,DEX,SPD,INT,MEN)            this.effect = EFFECT; // Bonus effect, array          }          var LargeAxe = new Weapon          ('Large Axe', 'axe', '', 12, '2h', 14, 10, [], []);          /***********************          ARMOURS CLASS          ***********************/          function Armour(NAME, TYPE, CLASS, DEF, WEIGHT, STATS, EFFECT) {            this.name = NAME; // Armour name            this.type = TYPE; // Armour type            this.class = CLASS; // Class restrictions, array            this.defense = DEF; // Defense            this.weight = WEIGHT; // Weight            this.stats = STATS; // Bonus stats, array(VIT,STR,DEX,SPD,INT,MEN)            this.effect = EFFECT; // Bonus effect, array                      }          var LeatherChest = new Armour          ('Leather Armour', 'chest', '', 8, 6, [], []);          var LeatherBoots = new Armour          ('Leather Boots', 'boots', '', 6, 3, [], []);
Link to comment
Share on other sites

Ok, if you moved:

console.log(Berserker);
to the top you would see that it is undefined until the interpreter gets to:

var Berserker = new Class(  'Berserker',   [10,10,10,10,10,10],   [90,110,80,80,70,70]);
 

 

So after trying the logging at various points, try moving the "var Berserker = ..." lines to above the "var player = new Hero" line  ;)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...