2016-07-17 37 views
-1

我在含colortext变量流星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,但我无法采用它的标记。如果常量colornull,false0,NaN"",我想要使用defaultColor

+1

'常量exampleTemplate =(文字,颜色= defaultColor)'是最好的做法 - 不知道为什么它会是NAN“”或假 - 上述情况下的测试,如果没有通过 - 你必须检查这些中代码 - 而且你的模板文字不正确 - 应该被back ticks包围,除非Meteor使用不同的模板引擎 - 但是假设这是es2015语法 – markyph

+0

“*我无法采用它的标记*” - 只是使用'... $ {颜色|| defaultColor} ...。 – Bergi

+0

'color'不是'const'?它是一个函数参数(存储在'const exampleTemplate'变量中的箭头函数),其行为就像一个'var'。 – Bergi

回答

1

您正在寻找Default Parameters

const exampleTemplate = (text, color = defaultColor) => (' 
'); 

您仍然需要对所提到的所有条件进行一些类型检查。