2013-10-20 65 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