Jump to content

help me to understadn increment ?


espace
 Share

Recommended Posts

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)
	}
}

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...