tags:

views:

65

answers:

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

+1  A: 

? is the ternary operator

it equals

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

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.

moonshadow
In his example, the `-=` and `+=` are actually strings.
Brian McKenna
@Brian: yeah, just spotted that :)
moonshadow
hmm... `-=` and `+=` as strings... I smell an `eval` somewhere ;)
Andy E
+1  A: 
  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.
KennyTM
A: 

There is a couple of videos by Douglas Crockford's that is absolutely amazing

Rihan Meij
A: 

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.

Ivan Karpan
A: 

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 -=

Andrew
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)
Dan Beam