Your code actually defines gradientChoice
as an object (that's what = {
does) however you can't have an if
inside an object-literal.
Instead, use the ternary-operator: <bool-expr> ? <when-true> : <when-false>
.
const gradientChoice = ( gradientSelected === "linear" ) ?
`linear-gradient(${degSlider}deg, ${firstColorInput}, ${secondColorInput})`
:
`radial-gradient(${degSlider}deg, ${firstColorInput}, ${secondColorInput})`
;
This can be simplified to this:
const gradientChoice = `${ gradientSelected === "linear" ? "linear-gradient" : "radial-gradient" }( ${degSlider}deg, ${firstColorInput}, ${secondColorInput} )`;
Personally I'd do it like this to reduce the total line-length:
const gradientFunc = gradientSelected === "linear" ? "linear-gradient" : "radial-gradient";
const gradientChoice = `${ gradientFunc }( ${degSlider}deg, ${firstColorInput}, ${secondColorInput} )`;
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…