javascript - Javascript字符串插值的使用方法
问题描述
我遇到了这个使用字符串插值的开发人员的例子。
他使用它的方式如下:
console.log(`Hello, $userName`);
直到后来在他的示例中,我才发现这有什么问题。他再次进行了字符串插值,但这次不同。这次他使用了括号。像这样:
console.log(`The user asked ${question}`);
有什么不同 ?我使用哪种方式有关系吗?还是仅仅是开发人员的错误。
解决方案
最简单的方法是尝试
如您所见,第一个没有做任何事情
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}`);
推荐阅读
- java - Possible to run two ContentHandlers for a single parse in Apache-Tika?
- java - 在Activity之间切换时如何避免白屏
- c++ - C++:如何将 std::vector 操作转换为 Eigen::VectorXf?
- javascript - 用于 sql 异常的正则表达式以查找约束名称
- azure - mcrosoft b2c 注册中的自定义 HTML 页面
- c++ - 多个rocksdb实例:使用单个共享缓存还是多个独立缓存?
- reactjs - ApexCharts 移除额外的底部填充
- google-pagespeed - Google pagespeed v5 是否删除了屏幕截图支持?
- python - 编码错误读取stata文件,read_stata()不接受编码参数python
- c++ - 未显式传递参数时使用 lambda 默认为用户输入