1
我在脚本的顶部有一个字符串,其中有一个变量,但尚未声明,但我想将字符串保留在脚本的顶部以使它更容易供其他用户修改。有没有办法做到这一点?例如:尚未声明的字符串变量
const greeting = `Hello, ${name}. How are you?`;
然后,在代码的一个完全不同的部分,其中上述greeting
没有name
访问:
const name = 'Steve';
sendMessage(greeting);
HTTPS:/ /stackoverflow.com/questions/42279852/format-string-template-with-variables-in-javascript/42280083#42280083 – pawel
阅读@OriDrori的答案或尝试定义'name'常量,然后再添加找到'greeting'常量的字符串。 –