2013-10-20 118 views
0

在JavaScript中給出的十六進制顏色值,它是在引號如「FCAA00」時(在Photoshop中)十六進制顏色在Javascript

var hexCol = "FCAA00"; 
var fgColor = new SolidColor; 
fgColor.rgb.hexValue = hexCol 

而是傳遞一個變量是值當你不需要引號。爲什麼是這樣?

var hexCol = convertRgbToHex(252, 170, 0) 
hexCol = "\"" + hexCol + "\""; // These quotes are not needed. 
var fgColor = new SolidColor; 
fgColor.rgb.hexValue = hexCol 

這只是一個javaScript怪癖嗎?或者我錯過了幕後的事情,因爲它是。謝謝。

+0

轉換中發生了什麼(我假設不是音樂會?)RgbToHex? –

+0

如果'convertRGBToHex()'返回一個字符串,那麼相當於'var hexCol =「FCAA00」;' –

+0

concertRgbToHex()這就是所有時髦數字在露天體育場內閒逛並聽音樂的地方。在十六進制中...(排版羞辱的Facepalm!) –

回答

3

引號是一個句法結構,表示字符串文字。即解析器知道引號之間的字符形成字符串的值。這也意味着它們是而不是的一部分值本身,它們只與解析器相關。

例子:

// string literal with value foo 
"foo" 

// the string **value** is assigned to the variable bar, 
// i.e. the variables references a string with value foo 
var bar = "foo"; 

// Here we have three strings: 
// Two string literals with the value " (a quotation mark) 
// One variable with the value foo 
// The three string values are concatenated together and result in "foo", 
// which is a different value than foo 
var baz = "\"" + bar + "\""; 

最後一種情況是你嘗試過什麼。它創建一個字符串字面上包含引號。這相當於寫作

"\"foo\"" 

這明顯不同於"foo"

+0

很好的答案。對於食屍鬼,我建議想想如果你*沒有在「FCAA00」上使用引號會發生什麼。解析器會查找名爲** FCAA00 **的變量,但無法找到它。事實上,你可以寫一些非常混亂的代碼,如FCAA00 =「00AA00」; – Jere