javascript 模板字符串中的默认值

tin*_*ino 5 javascript template-strings

有没有办法访问 javascript 模板字符串引擎来为未定义的变量提供默认值?

console.log(`this variable is undefined: ${x}`) 
// throws ReferenceError

// but i want to generate something like this:
"this variable is undefined: <warning! undefined variable>"
Run Code Online (Sandbox Code Playgroud)

这也可以:

function tag(strings,...values){
   // values[i] should be "undefined" if this variable is undefined
}
tag`${x}`
Run Code Online (Sandbox Code Playgroud)

如果那是不可能的,是否有一个模板字符串引擎可以完全满足 javascript 的功能并具有此功能?

Ayu*_*pta 6

您可以使用||来合并值,例如:

console.log(`this variable is undefined: ${x || '<warning! undefined variable>'}`) 
Run Code Online (Sandbox Code Playgroud)