模板字符串结合三目运算

86 阅读1分钟
<script>
  const username = "xiaoming";
  const person = {
    age: 19,
    sex: "male",
  };
  //   定义一个函数,转换性别,如果是male,就输出男,否则输出女
  const getSex = function (sex) {
    return sex === "male" ? "男" : "女";
  };

  //   都定义好了,下面直接调用

  const result = `大家好,我叫${username},今年${person.age}岁,是个${getSex(
    person.sex
  )}生`;
  console.log(result);
</script>