不确定如何在JavaScript中使用反引号

问题描述 投票:0回答:1

这是我第一次使用反引号,但我的功能无法在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 google-chrome firefox atom-editor
1个回答
0
投票

使用反引号就好了。 JavaScript解释器抱怨,因为它不知道helloworld。您将它们作为变量传递,而需要将它们作为字符串传递。像这样:

function makeLetter(fName, lName) {
  return `Dear ${fName} ${lName},
    How are you today?`;
}

console.log(makeLetter('John', 'Doe'));

提示:由于您的函数返回了一个字符串,因此我使用了console.log打印出消息。

© www.soinside.com 2019 - 2024. All rights reserved.