简体   繁体   English

函数值到底是什么?

[英]What precisely is a function value?

In JavaScript (ECMAScript 5), functions are valued (they are told to be "first-class functions"). 在JavaScript(ECMAScript 5)中,函数是有价的(被告知它们是“一流的函数”)。

This allows us to use them as expressions (an expression is everything which produces a value, and can contain other expressions : var exp0 = (exp1) + exp2 - exp3.function(); is a grammar-correct statement). 这使我们可以将它们用作表达式(表达式是产生值的所有内容,并且可以包含其他表达式: var exp0 = (exp1) + exp2 - exp3.function();是语法正确的语句)。

In the code above, there are 8 expressions : exp0, exp1, (exp1), exp2, (exp1) + exp2, exp3, exp3.function() and (exp1) + exp2 - exp3.function(). 在上面的代码中,有8个表达式:exp0,exp1,(exp1),exp2,(exp1)+ exp2,exp3,exp3.function()和(exp1)+ exp2-exp3.function()。


Because functions can be used as expressions, the following code is correct : 因为函数可以用作表达式,所以以下代码是正确的:

var my_function_0 = function a() {} is a named function expression. var my_function_0 = function a() {}是一个命名函数表达式。

The following code is also correct : 以下代码也是正确的:

var my_function_1 = function() {}` is an anonymous function expression. var my_function_1 = function() {}`是匿名函数表达式。

Both are valued, both are values. 两者都是有价值的,都是价值。


Now, consider the code below : 现在,考虑以下代码:

function requiredIdentifier() {}

It is NOT a "named or anonymous function expression", but a function declaration. 它不是“命名或匿名函数表达式”,而是函数声明。


My question is : 我的问题是:

Does a declared function have/produce a value ? 声明的函数是否具有/产生值?

This question is equivalent to this one : Is a declared function an expression ? 这个问题等于这个问题: 声明的函数是表达式吗? (even if it's not a named or anonymous function expression ?!) (即使它不是命名或匿名函数表达式?!)

Does a declared function have/produce a value? 声明的函数是否具有/产生值?

Yes. 是。 Regardless what syntax is used to create the function, a function is a callable object (ie it implements an internal interface that makes it callable): 无论使用什么语法来创建函数,函数都是可调用对象(即,它实现了使其可调用的内部接口):

function a() {}
var b = function() {}
var c = (new Function()) // or some other expression that returns a function

All of the variables a , b and c hold a function value. 所有变量abc具有函数值。

The difference between the syntaxes is only when the value is created and whether/when it is bound to a variable. 语法之间的区别仅在于创建值以及是否/何时将其绑定到变量时。 See var functionName = function() {} vs function functionName() {} for those details. 有关这些详细信息,请参见var functionName = function(){}与function functionName(){}

A function declaration is a statement, not an expression. 函数声明是语句,而不是表达式。 Since statements can't be used in expressions, it's pointless to ask what its value is, since there's no way to use the value. 由于语句不能在表达式中使用,因此询问其值是毫无意义的,因为无法使用该值。

However, the syntax of a function declaration is identical to that of a named function expression. 但是,函数声明的语法与命名函数表达式的语法相同。 So if you use it anywhere that an expression is required, it will be treated as a named function expression, and the value will be the function. 因此,如果在需要表达式的任何地方使用它,它将被视为命名函数表达式,并且值将是函数。

For example: 例如:

(function requiredIdentifier() {})

is a parenthesis expression containing a named function expression, not a function declaration. 是包含命名函数表达式而不是函数声明的括号表达式。 Its value is the function that was defined. 它的值是定义的功能。

好吧,如果您运行函数requiredIdentifier()则返回值是undefined ,而undefined实际上是javascript(ecma脚本)中的原始数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM