【问题标题】:Why wont I receive both the return statement as well as the console.log statement? [closed]为什么我不会同时收到 return 语句和 console.log 语句? [关闭]
【发布时间】:2023-03-26 11:06:01
【问题描述】:

为什么我不能同时获得 return 语句和 console.log 语句结果?

console.log 输出“Emily”,但不会输出文本“说,喝咖啡太棒了!”

function coffeeDrink(){
    return this.name;
    console.log("says, that excessive coffee is awesome!")
}

var literalMallory = {
    name         : "Mallory",
    favDrink     : coffeeDrink
}

var literalEmily = {
    name        : "Emily",
    favDrink    : coffeeDrink
}

coffeeDrink.call(literalEmily) //outputs "Emily"

我期待它说: “艾米丽说,过量的咖啡太棒了!”

【问题讨论】:

标签: javascript javascript-objects


【解决方案1】:

你需要

function coffeeDrink(){
    console.log(this.name + " says, that excessive coffee is awesome!");
    return this.name;
}

return 语句退出执行,将其作为最后一条语句将使console.log 工作,并将名称返回给被调用者

【讨论】:

  • 嘿,谢谢!我是一个初学者,是的,我只知道 return 会返回一些东西,但我不知道它会立即退出函数,即使我可能在几个月前就已经学会了。非常感谢
猜你喜欢
  • 2021-02-12
  • 2017-03-17
  • 1970-01-01
  • 2022-12-09
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
  • 2022-11-20
  • 2021-09-10
相关资源
最近更新 更多