这是我第一次使用反引号,但我的功能无法在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'));
提示:由于您的函数返回了一个字符串,因此我使用了console.log
打印出消息。