2014-01-16 42 views
-1

我有兩個問題:JavaScript的語法?和:

我有一個JavaScript函數代碼:

var firstOrNull = function (elems){ 
    return (elems.length > 0) ? elems[0] : null; 
} 

什麼?:意味着這個代碼?

2.這是什麼代碼的含義:

var stopEvent = function(event){ event.stopPropagation() } 
+0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator – Blender

+3

http://stackoverflow.com/questions/10323829/javascript-ternary-operator-example -with-functions –

+0

stoppropogation呢? –

回答

0

? and :對顯示在Javascript中三元運算符。

(elems.length > 0) ? elems[0] : null;線意味着如果elems length大於zero然後返回elems[0]否則返回null

0

這就是所謂的三元運營商

if(elements.length > 0){ 
    return elems[0]; 
} else { 
return null; 
} 

相當於:

return (elems.length > 0) ? elems[0] : null; 

ternary operators