首页 > 解决方案 > 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");
};

标签: javascriptfunction

解决方案


那是一个模板文字/模板字符串${and}是定义占位符的标记,这些占位符将被其中的表达式的值替换。

所以这:

console.log(`${ ingredientAmount } ${ unit } ${ name }`);

正常string情况下是:

console.log(ingredientAmount + ' ' + unit + ' ' + name);

推荐阅读