espace Posted September 11, 2018 Share Posted September 11, 2018 hi, i don't understand the principle of increment with javascript. This snippet works well : //in update f.big=(obj,speed)=>{ if (obj.scale.x < 8) { obj.scale.setTo(obj.scale.x + speed) } } but this example don't work however it's supposed to be the same...why that don't works ? // in update f.pointer_big=(obj,speed)=>{ if (obj.scale.x < 8) { obj.scale.setTo(obj.scale.x++ * speed) } } Quote Link to comment Share on other sites More sharing options...
tips4design Posted September 11, 2018 Share Posted September 11, 2018 What are you trying to do in the second case? What is target value? Quote Link to comment Share on other sites More sharing options...
espace Posted September 11, 2018 Author Share Posted September 11, 2018 i would the same than the one case. Quote Link to comment Share on other sites More sharing options...
mattstyles Posted September 12, 2018 Share Posted September 12, 2018 Your 2 examples aren't the same, and I can't see why you'd think they would be comparable. I can see why you'd be confused by the output of the 2nd though, but if you are confused by the output then its to do with the order of operations. Postfix operators (generally) have lower precedence so they happen after other operands, prefix operators (generally) are executed before. i.e. var x = 10 x++ + 2 => 10 + 2 => change x // 12, x === 11 x = 10 ++x + 2 => change x => 11 + 2 // 13, x === 11 I'm not sure on the exact order of operands in JS, but I'd pretty much expect prefix to happen first and postfix to happen last in most expressions. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.