-1
我在含color
和text
变量流星const
:如何设置const的默认值?
const exampleTemplate = (text, color) => ('
Hello, this is your text:
${text}.
I hope you liked it. And here comes your color:
<span style="color: ${color}">Boom, here she is.</span>
');
const defaultColor = '#123456';
const firstColor = '#876543';
const secondColor = '#295736';
return exampleTemplate('I am a text example', defaultColor);
我如何定义defaultColor
为默认值,如果没有其他的变种设置?我找到了this thread,但我无法采用它的标记。如果常量color
是null
,false
,0
,NaN
或""
,我想要使用defaultColor
。
'常量exampleTemplate =(文字,颜色= defaultColor)'是最好的做法 - 不知道为什么它会是NAN“”或假 - 上述情况下的测试,如果没有通过 - 你必须检查这些中代码 - 而且你的模板文字不正确 - 应该被back ticks包围,除非Meteor使用不同的模板引擎 - 但是假设这是es2015语法 – markyph
“*我无法采用它的标记*” - 只是使用'... $ {颜色|| defaultColor} ...。 – Bergi
'color'不是'const'?它是一个函数参数(存储在'const exampleTemplate'变量中的箭头函数),其行为就像一个'var'。 – Bergi