2016-05-30 80 views
-3

讀highstock源代碼,但混淆以下代碼:什麼是&&和||在下面的代碼的意思

textLineHeight = textStyles && textStyles.lineHeight, 

wrapper.height = (height || bBox.height || 0) + 2 * padding; 

cHeight = (old && chart.oldChartHeight) || chart.chartHeight; 

由於

+0

JavaScript和(&&)運營商或符(||) http://www.w3schools.com/js/js_operators.asp – brk

+0

@ user2181397,他要求 「在下面的代碼」 – slick

+0

@slick,運營商改變關於代碼,這是你的意思? – SilentMonk

回答

3
textLineHeight = textStyles && textStyles.lineHeight; 

裝置設定textLineHeighttextStyles.lineHeight如果textStyles是truthy,它有一個屬性lineHeight這是也truthy。

&&通常被稱爲JavaScript中的警衛操作符,因爲其短路評估確保textStyles.lineHeight不會被評估,除非定義了textStyles。這可以避免一個錯誤,如果它確實可能是未定義的。

wrapper.height = (height || bBox.height || 0) + 2 * padding; 

這裏,||手段使用height如果是truthy以其它方式使用bBox.height如果這是truthy,最後退回到0如果也不是truthy。 ||通常被稱爲JavaScript中的默認運算符,因爲它允許指定一個值,以便在左邊的表達式是虛假的情況下使用。

相關問題