6

I am trying upgrade my javascript programming skills ( or lets say my programming skills period : ) )

so I am trying to understand some semantics :

in the first line what does the "?" mean as well as the minus sign in "-distance"

in the second line what does '+=' or '-=" mean?

 el.css(ref, motion == 'pos' ? -distance : distance)

animation[ref] = (mode == 'show' ? (motion == 'pos' ? '+=' : '-=') : (motion == 'pos' ? '-=' : '+=')) + distance;

thank you

4
  • 1
    Want to improve your programming skills? After you undestand what it does, rewrite it in a readable way that will not confuse the next coder. (btw, it looks like utter nonsense - it is playing with +/- of a number, and then subtracts or add it, seems pointless) Commented Jan 27, 2010 at 9:22
  • yes, like this, el.css(ref, -1 * ('pos' === motion) * distance), :P (just a joke) Commented Jan 27, 2010 at 9:27
  • @Dan: you want (1-('pos'===motion)*2)*distance. Commented Jan 27, 2010 at 9:29
  • yes, you're right. the readability is so good even I can interpret it, :D Commented Jan 27, 2010 at 9:32

5 Answers 5

5

a ? b : c means "b if a is true, c otherwise".

-a means a, negated.

a -= b and a += b mean a = a - b and a = a + b respectively. However, in your example these operators aren't actually present in the code, they are just text strings the code is manipulating.

Sign up to request clarification or add additional context in comments.

3 Comments

In his example, the -= and += are actually strings.
@Brian: yeah, just spotted that :)
hmm... -= and += as strings... I smell an eval somewhere ;)
1

? is the ternary operator

it equals

if( motion == 'pos' ) { return -distance; } else { return distance; } // - is just negating the distance value

Comments

1
  1. (a ? b : c) means "return b if a is true, and return c if a is false."
  2. The minus sign means negation.
  3. The '+=' and '-=' are simply strings.

Comments

0

What you call 'semantics' is actually programming language syntax. It's very basic knowledge that can be acquired easily by googling a bit or looking at Wikipedia.

Here's the JavaScript article on Wikipedia, and here's the answers on your first (conditional operator section), second (Arithmetic) and third (Assignment) questions within the same article. RTFM please.

Comments

0

Here is a link that will answer the ? question (? is a shorthand evaluation operation). http://www.w3schools.com/JS/js_comparisons.asp

+= would be used to increment a value (also shorthand) e.g.

i = i + 1; is the same as i += 1;

the same applies to -=

1 Comment

technically I guess this is correct, but '-=' is not the same as simple -= (it's a string sent to jQuery's css function, but is doing this behind the scenes, so I didn't vote you down)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.