javascript semantics
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' ? '+=' : '-='开发者_Go百科) : (motion == 'pos' ? '-=' : '+=')) + distance;
thank you
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.
? is the ternary operator
it equals
if( motion == 'pos' ) { return -distance; } else { return distance; } // - is just negating the distance value
(a ? b : c)
means "returnb
ifa
is true, and returnc
ifa
is false."- The minus sign means negation.
- The
'+='
and'-='
are simply strings.
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.
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 -=
精彩评论