函数表达式与函数声明:返回值 [英] Function expressions vs function declarations: return value

查看:129
本文介绍了函数表达式与函数声明:返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Udacity课程中,函数表达式和声明之间的区别解释如下:

In a Udacity lesson the difference between function expressions and declarations is explained as follows:


函数声明定义一个函数而不是需要为其分配
变量。它只是声明一个函数,
本身不返回一个值...另一方面,函数
表达式确实返回一个值。

A function declaration defines a function and does not require a variable to be assigned to it. It simply declares a function, and doesn't itself return a value ... On the other hand, a function expression does return a value.

这令人困惑;据我所知,当函数表达式和函数声明都包含return语句时,都返回一个值。

This is confusing; to my knowledge, when both function expressions and function declarations include a return statement, both return a value.

如果我理解正确,返回值的差异在于函数表达式中,如果在函数的第一次调用中更改了值,则在后续调用更新后的值将被保留 - 而如果函数未存储在变量中,则返回值将在函数执行完毕后被删除。我错过了什么,并且该课程中的陈述是否准确?

If I understand correctly, the difference with respect to return value is that in a function expression, if a value is changed in the first call of the function, in a subsequent call the updated value would be preserved—whereas if the function were not stored in a variable, the return value would be erased when the function is finished executing. Am I missing something, and is the statement from the lesson accurate?

注意:我的问题与标记为重复的问题不同。在那个问题中,它询问使用其中一个的原因是什么,但问题中没有提到或在答案中解释返回值。

Note: My question is different from the one marked as a duplicate. In that question it asks what the reasons for using one over the other, but return value is not mentioned in the question or explained in its answers.

推荐答案



另一方面,函数表达式确实返回一个值。

On the other hand, a function expression does return a value.

这令人困惑

确实如此。它们的实际含义是函数表达式求值为(函数)值 - 与声明形成对比,声明不是表达式而是语句,并且不计算任何值。它与函数可能从调用返回的值无关。

Yes indeed. What they actually meant was a function expression evaluates to a (function) value - in contrast to a declaration, which is not an expression but a statement and doesn't evaluate to anything. It has nothing do with the value that the function might return from a call.

这篇关于函数表达式与函数声明:返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆