Home > Enterprise >  How to change a local class varible node js
How to change a local class varible node js

Time:12-05

I don't really know how to explain this but I will try my best

so I have a Card class in the file card.js that look like this:

class card {
    constructor(name = "", portrait = "", bloodCost = 0, boneCost = 0, power = 0, health = 1, sigilList = []) {
        this.name = name
        this.portrait = portrait;
        this.bloodCost = bloodCost;
        this.boneCost = boneCost;
        this.power = power;
        this.health = health;
        this.sigilList = sigilList;
    }
    attack(sigilList = [], board = [], oppositeCard = new card(), scale = 0) {
        var oppositeCardHealth = 0
        if (oppositeCard == blank) {
            scale  = this.power;
        }
        else {
            oppositeCardHealth  = this.power;
        }
        return [scale, oppositeCardHealth];
    };

var wolf = new card("Wolf", ":wolf:", 2, 0, 3, 2);
module.exports = { wolf };
}

And a 'main.js' file like this:

const cardLib = require("./lib/card");
var broad = [cardLib.wolf, cardLib.wolf]
broad[0].health -= 2;
console.log(broad);

So what I want to do is only changing the health of the wolf in broad[0] with out changing the other one health. Which mean the program should return something like this:

[
  card {
    name: 'Wolf',      
    portrait: ':wolf:',
    bloodCost: 2,      
    boneCost: 0,       
    power: 3,
    health: 0,
    sigilList: []      
  },
  card {
    name: 'Wolf',      
    portrait: ':wolf:',
    bloodCost: 2,      
    boneCost: 0,
    power: 3,
    health: 2,
    sigilList: []
  }
]

CodePudding user response:

This statement:

var broad = [cardLib.wolf, cardLib.wolf]

Put's two references to the exact same wolf object in your array. So, naturally, if you change one, the other will appear changed also because both spots in the array point at the exact same object.

If you want two separate, independent objects in the array, then you have to create two separate objects by creating a separate card. To do that from outside that file, it would be best to export the Card constructor so you can call it and create a separate card to put into the array. Another option would be to add a .copy() method to your Card object so you can create a copy of an existing object.

CodePudding user response:

In your current code, wolf is a specific instance of a card. Your array has two references to the same object, so changing one of them will change them "both".

One approach could be to not hold a wolf variable, but instead provide a function to create such a wolf card:

// card.js:
class card {
     // constructor and attack unchanged, removed for brevity's sake
}

var wolf = () => new card("Wolf", ":wolf:", 2, 0, 3, 2);
module.exports = { wolf };

// main.js:
const cardLib = require("./lib/card");
var broad = [cardLib.wolf(), cardLib.wolf()]; // wolf is a function that creates a a card
broad[0].health -= 2;
console.log(broad);
  • Related