0

I have two files, a main file and a module file. Inside the module file, I export two functions, which return objects. In the main file, I am setting a variable to the return value of each method. However, each variable is set to the return value of the last method called. What am I doing wrong here?

/* main file */
module.exports = function(robot) {
  var startingCharacter = require('./data/starting/character.json'),
      make = require('./includes/make.js');

  return robot.respond(/dungeon me/i, function(msg) {
    var character = startingCharacter,
        weapon = make.weapon(0),
        armor = make.armor(0);

    console.log(weapon); // <- returns value from make.armor(0)
    console.log(armor); // <- returns value from make.armor(0)
  }
};


/* module file */
var random = require('./random.js');

var baseStats = {
  'equipped': 'true',
  'attack': '0',
  'damage': '0',
  'defense': '0',
  'health': '0',
  'luck': '0',
  'passivePower': {},
  'activePower': {}
};

// Generate a weapon
exports.weapon = function(level) {
  var weaponNames = require('../data/nouns/weapon_names.json');
  var thisWeapon = {};

  if (level === 0) {
    var adjNeg = require('../data/adjectives/negative.json');

    thisWeapon = baseStats;
    thisWeapon.name = adjNeg[random(0, adjNeg.length)] + ' ' +  weaponNames[random(0, weaponNames.length)];
    thisWeapon.type = 'weapon';
  }

  console.log(thisWeapon); // <- returns correct value
  return thisWeapon;
};

// Generate armor
exports.armor = function(level) {
  var armorNames = require('../data/nouns/armor_names.json');
  var thisArmor = {};

  if (level === 0) {
    var adjNeg = require('../data/adjectives/negative.json'),
        animals = require('../data/nouns/animals.json');

    var armorName = armorNames[random(0, armorNames.length)];

    thisArmor = baseStats;

    if (armorName === 'Hide') {
      thisArmor.name = animals[random(0, animals.length)] + ' ' + armorName + ' armor';
    }
    else {
      thisArmor.name = adjNeg[random(0, adjNeg.length)] + ' ' + armorName;
    }
    thisArmor.type = 'armor';
  }

  console.log(thisArmor); // <- returns correct value
  return thisArmor;
};

Note: edited with real code, instead of sample code

3
  • The code as posted has errors in it - the "method2" code as written here declares "myObject1" but uses "myObject2". When that's fixed, the code works fine.
    – Pointy
    Apr 5, 2015 at 20:32
  • Pointy - You're right. I cooked up sample code, thinking it would be easier for people to look at, which I made an error in, and fixing it does work. I'll replace the sample code with the actual code I'm using, which does have the behavior I described. Apr 5, 2015 at 20:54
  • OK, thanks. That certainly helps.
    – Pointy
    Apr 5, 2015 at 21:07

1 Answer 1

1

When you do this:

thisArmor = baseStats;

you're not making a copy of the "baseStats" object. Since both methods in the module modify and return a reference to that same object, you get the effect you observe.

There are several ways to implement a shallow copy operation for Node; see this Stackoverflow question. With one of those approaches, you'd start those methods off with a local copy of "baseStats".

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.