Search code examples
javascriptoopscopes

Values not changing correctly, same for variable


I am making a small game (like a cookie clicker but a CS:GO Version) where I had to start learning OOP so I'm really new to this programing type and at some point, a problem occurs.

I'm pretty sure this is a scope problem but how to solve this ?

Here is my code (I'll explain further on my problem):

The prototype:

function amelioration(Class1, Class2, Nom, Nombre, Prix, Ajout1, Ajout2) {
    this.Class1 = Class1;
    this.Class2 = Class2;
    this.Nom = Nom;
    this.Nombre = Nombre;
    this.Prix = Prix;
    this.Ajout1 = Ajout1;
    this.Ajout2 = Ajout2;
}

    var glockClicker = new amelioration(".glockBtn .clickerUpgInpt1", ".glockBtn .clickerUpgInpt2", "glockClicker", 0, 20, 1, 1);

The function (with the problem):

function buy(varAmelioration, Class1, Class2, Nom, Nombre, Prix, Ajout1, Ajout2, Multiplicateur) {
        if (euroCount >= Prix) {
            Nombre += 1;
            euroCount -= Prix;
            Prix *= Multiplicateur;
            euroPerSec += Ajout1;
            euroPerClick += Ajout2;
            $("#caseSpot .casePerSec").val(euroPerSec);
            $(Class1).val(Nombre);
            Prix = parseInt(Prix);
            $(Class2).val(Prix);
            varAmelioration = new amelioration(Class1, Class2, Nom, Nombre, Prix, Ajout1, Ajout2);
        }
    }

The event:

$(".glockBtn").click(function () {
        buy(glockClicker, ".glockBtn .clickerUpgInpt1", ".glockBtn .clickerUpgInpt2", glockClicker, glockClicker["Nombre"], glockClicker["Prix"], glockClicker["Ajout1"], glockClicker["Ajout2"], 1.5);
    });

So now that you have all of this here is my problem. When I trigger "the event", "the prototype" does its thing then "the function buy()" does also its thing.

But when I retrigger a second time "the event" nothing changed, all the values didn't change (price = "Prix" is still the same, etc...) and I don't understand why, I tried to assign a new value to the variable "glockClicker" that you can find in "the prototype" but still doesn't change the values, I would like help to find what isn't right or help for a way to change values of an objet like if it was a variable.

I hope I'm clear enough if not just tell me what you didn't understand.

Short version of my code :

function f(x) {
    this.x = x;
}
var a = new f(1);
console.log(f);

function b(myVar) {
    myVar += 1;
    console.log(myVar);
}

b(a["x"]);
console.log(a);
console.log(a["x"]);

b(a["x"]);
console.log(a);
console.log(a["x"]);

Solution

  • Code not working:

    function f(x) {
    this.x = x;
    }
    var a = new f(1);
    console.log(f);
    
    function b(myVar) {
        myVar += 1;
        console.log(myVar);
    }
    
    b(a["x"]);
    console.log(a);
    console.log(a["x"]);
    
    b(a["x"]);
    console.log(a);
    console.log(a["x"]);
    

    Code that is working:

    function f(x) {
    this.x = x;
    }
    var a = new f(1);
    console.log(f);
    
    function b(myVar, val) {
        myVar[val] += 1;
        console.log(myVar[val]);
    }
    
    b(a, "x");
    console.log(a);
    console.log(a["x"]);
    
    b(a, "x");
    console.log(a);
    console.log(a["x"]);