不确定如何在 JavaScript 中使用反引号
Not Sure How To Use Backticks in JavaScript
这是我第一次使用反引号,我无法让我的函数在 FireFox 或 Chrome 中运行。这是我的代码:
function makeLetter(fName, lName) {
return `Dear ${fName} ${lName},
How are you today?`;
}
当我输入 makeLetter(hello, world)
时出现此错误:
ReferenceError: hello is not defineddebugger eval code:1:1
<anonymous> debugger eval code:1
我做错了什么?
反引号用得很好。 JavaScript 解释器抱怨,因为它不知道 hello
或 world
。您将它们作为变量传递,您需要将它们作为字符串传递。像这样:
function makeLetter(fName, lName) {
return `Dear ${fName} ${lName},
How are you today?`;
}
console.log(makeLetter('John', 'Doe'));
提示:由于你的函数 returns 是一个字符串,所以我使用 console.log
来打印消息。
这是我第一次使用反引号,我无法让我的函数在 FireFox 或 Chrome 中运行。这是我的代码:
function makeLetter(fName, lName) {
return `Dear ${fName} ${lName},
How are you today?`;
}
当我输入 makeLetter(hello, world)
时出现此错误:
ReferenceError: hello is not defineddebugger eval code:1:1
<anonymous> debugger eval code:1
我做错了什么?
反引号用得很好。 JavaScript 解释器抱怨,因为它不知道 hello
或 world
。您将它们作为变量传递,您需要将它们作为字符串传递。像这样:
function makeLetter(fName, lName) {
return `Dear ${fName} ${lName},
How are you today?`;
}
console.log(makeLetter('John', 'Doe'));
提示:由于你的函数 returns 是一个字符串,所以我使用 console.log
来打印消息。