By registering with us, you'll be able to discuss, share and private message with other members of our community.
SignUp Now!var var1 = 12;
to var var1 = 4
, so could you please explain a bit more what you're trying to dolet dict = { pos: [1,1],[2,2] ,pos2: [3,3],[4,4]};
let pawn = class {
constructor {
this.int = 0; // index for pos1
this.anotherint = 0; // index for pos2
this.position = [0,0]; // screen position (in pixel in real code)
this.type_int = ["pos1",this.int];
go_forward() {
this.position = dict[0][type_int[0]]type_int[1][0]],dict[0][type_int[0]][type_int[1][1]];
drawPawn(this.position);
}
}
}
obj.type_int = ["pos2",obj.anotherint];
obj.go_forward(); // drawPawn will draw in [3,3]
..
obj.anotherint++; // new value = 1
obj.go_forward(); //drawPawn still draws in [3,3]
..
console.log(this.position); // return [3,3]
//expected output : [4,4] obj.anotherint doesn't update in obj.type_int, it stays 0
this.position = dict[0][type_int[0]]type_int[1][0]],dict[0][type_int[0]][type_int[1][1]];
constructor
. The horrible line also has errors, I can see right away the brackets are unbalanced. Did you write this code yourself ? Then try something less complicated.var2
was already evaluated on line 2, using the then current value of var1
. How would you expect it to change when you then re-assign var1
? Note that you actually re-declare it, you have var var1
twice. Not sure if that is part of the issue, but don't do this kind of thing. You just can't be sure you are looking at the same memory address.eval()
function (about which many people say "don't use it") but I could not get it to work like you want. I guess what you need is some kind of lazy evaluation, but when you read up on that (e.g. here https://www.codementor.io/@agustinchiappeberrini/lazy-evaluation-and-javascript-a5m7g8gs3) it does get pretty abstract and complicated. Not recommended for a beginning programmer. So maybe you'll need to re-think your design.Code Forum is a community platform where coding enthusiasts can connect with other developers, engage in discussions, ask for help, and share their knowledge with a supportive community. It's a perfect place to improve your coding skills and to find a community of like-minded individuals who share your passion for coding.
We use essential cookies to make this site work, and optional cookies to enhance your experience.