0

在 javaScript(在 Photoshop 中)中,当给一个十六进制颜色一个值时,它用引号括起来,例如“FCAA00”

 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 怪癖还是我错过了幕后发生的事情,就像它一样。谢谢你。

4

1 回答 1

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".

于 2013-10-20T08:26:12.513 回答