javascript - javascript符号$的含义?
问题描述
请原谅我,这在javascript程序文本中是什么意思
console.log(`${ingredientAmount} ${unit} ${name}`); ?
这是包含前面提到的行的文本程序:
const hummus = function(factor) {
const ingredient = function(amount, unit, name) {
let ingredientAmount = amount * factor;
if (ingredientAmount > 1) {
unit += "s";
}
console.log(`${ingredientAmount} ${unit} ${name}`);
};
ingredient(1, "can", "chickpeas");
ingredient(0.25, "cup", "tahini");
ingredient(0.25, "cup", "lemon juice");
ingredient(1, "clove", "garlic");
ingredient(2, "tablespoon", "olive oil");
ingredient(0.5, "teaspoon", "cumin");
};
解决方案
那是一个模板文字/模板字符串,${
and}
是定义占位符的标记,这些占位符将被其中的表达式的值替换。
所以这:
console.log(`${ ingredientAmount } ${ unit } ${ name }`);
正常string
情况下是:
console.log(ingredientAmount + ' ' + unit + ' ' + name);
推荐阅读
- rabbitmq - 如何使用 Spring Cloud Dataflow 实现 RabbitMQ DLQ
- javascript - 复制 Python 工作流程以在 Javascript 环境中为 Tensorflow 预处理图像
- java - apache ignite 配置自定义缓存存储
- python - Python Pandas SQL 查询错误 - 模块“时间”没有属性“时钟”
- typescript - Vue 3 - Composition Api - Typescript -> 发射类型
- java - 将 OpenJDK 版本从 11.0.2 更新到 11.0.10 时发生类转换异常
- reactjs - 在 React MUI DataGrid 中的复选框选择上获取行项目
- python - 当应用程序的属性(例如:'类名')将来发生变化时,如何正确处理 Selenium Xpath
- css - 如何防止 CSS 动画移动我页面的其余部分?
- php - 使用 TCPDF 库创建 PDF 失败