这是一段代码,注意我已经评论了我想在代码中实现的目标

const extraHot = () => {
   if (hot === "h") {
      return `extra hot`
   }
// otherwise, do nothing, don't return anything
// (not even an empty string or undefined object)
// I want to break here
}

if (givenAmount >= dict){
// I would like to return "Drink maker makes 1 extra hot sugar and 1 stick"
    return `Drink maker makes 1 ${extraHot()} sugar and 1 stick`
}


以下代码将在“ makes”和“ sugar”之间返回“ undefined”

const extraHot = () => {
   if (hot === "h") {
      return `extra hot`
   }
   return
}


以下代码将返回“ makes”和“ sugar”之间的空格

const extraHot = () => {
   if (hot === "h") {
      return `extra hot`
   }
   return ""
}

最佳答案

您的问题不是函数hot !== "h"返回的内容,而是如何编写模板字符串。如果if的值为true,则返回' extra hot ',然后将模板字符串写为

Drink maker makes 1${extraHot()}sugar and 1 stick

关于javascript - 如何突破JavaScript中的功能?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59266509/

10-11 01:26