我试图在代码学院学习javascript,有人可以解释这个函数末尾“return 1;”的目的

Im trying to learn javascript on Code Academy could someone explain the purpose of "return 1;" at the end of this function

本文关键字:函数 return 解释 代码 学习 javascript      更新时间:2023-09-26
function dogCare()
{
    alert("walk 9am: twice around the block");
    alert("Feed at 4pm: Meat & Water");
    alert("walk at 10pm: once around the block");
    return 1;
}

为什么在函数的末尾有一个return 1;,它的目的是什么。

从中调用该函数的位置,它将返回 1 给调用方。

例如

var x = dogCare();
console.log('DogCare returned ' + x);

在 Firefox 中运行 Firebug 以查看控制台输出。

PS:我认为你应该每天至少喂狗两次!

它将值1返回到调用该函数的任何代码段。因此:

var foo = dogCare();
// foo is 1

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

数字"1"等效于布尔值"true"。我只能假设某处有一个检查可以从函数"成功"返回。