...返回Math.random()含义?

发布于 2025-01-17 17:33:38 字数 233 浏览 9 评论 0原文

每个人。

当我看到此代码时,我会感到困惑。 此代码是什么意思?为什么在返回此处之前使用传播操作员?

我尝试了使用节点的代码,它说意外的令牌'return'

fyi,此代码段来自nodejs文档。

function generateRandom() {
...return Math.random()
}

感谢任何解释。

everyone.

I am confused when I see this code.
What does this code mean? Why spread operator is used before return here?

I tried this code with node, and it says Unexpected token 'return'

Fyi, this code snippet is from nodejs document.

function generateRandom() {
...return Math.random()
}

Appreciate for any explanation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满栀 2025-01-24 17:33:38

这些点实际上并不是代码的一部分。它们只是 Node REPL 的一部分,在用户按下 Enter 后向用户发出指示,表明您键入的语句不完整。正如您找到此代码的链接所示

Node REPL 足够智能,可以确定您尚未编写完代码,并且它将进入多行模式,以便您输入更多代码。

也就是说,如果你开始输入:

function generateRandom() {

然后按回车键,你会看到以下内容

function generateRandom() {
...

,文本光标位于 ... 的末尾,表明你需要在 REPL 之前完成该语句执行它。

因此,

function generateRandom() {
...return Math.random()
}

当您取出 REPL 的视觉指示器时,表明您处于多行模式,这在

function generateRandom() {
return Math.random()
}

语法上是正确的。

Those dots are not actually part of the code. They are a part of the Node REPL only, an indicator to the user, after they press enter, that the statement you've typed is not complete. As the link where you found this code says:

The Node REPL is smart enough to determine that you are not done writing your code yet, and it will go into a multi-line mode for you to type in more code.

That is, if you start typing:

function generateRandom() {

and then press enter, you'll see the following

function generateRandom() {
...

with the text cursor at the end of the ..., indicating that you need to finish the statement before the REPL executes it.

So

function generateRandom() {
...return Math.random()
}

when you take out the REPL's visual indicator that you're in multi-line mode, is just

function generateRandom() {
return Math.random()
}

which is syntactically correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文