[英]Javascript string interpolation ways to use
I came across this example of a developer where he used string interpolation. 我遇到了一个使用字符串插值的开发人员示例。
The way he used it was as follow : 他使用它的方式如下:
console.log(`Hello, $userName`);
I didn't see anything wrong with that until later in his example. 直到稍后的示例中,我都没有发现任何问题。 He did string interpolation again but differently this time. 他再次进行了字符串插值,但是这次有所不同。 This time he used brackets. 这次他用了方括号。 like this: 像这样:
console.log(`The user asked ${question}`);
What's the difference ? 有什么不同 ? Does it matter which way I use? 使用哪种方式有关系吗? or is it simply a mistake from the developer. 还是仅仅是开发人员的错误。
Easiest way is to try 最简单的方法是尝试
As you can see the first does not do anything 如您所见,第一个没有做任何事情
var $userName = "Mike", userName = "Michael", question = "What's up?" console.log(`Hello, $userName`); // not a valid JS string interpolation console.log(`The user asked ${question}`); // This one works // Perhaps he meant console.log(`Hello`, $userName); // or more likely console.log(`Hello, ${userName}`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.