javascript 赋值返回的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16027247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:58:44  来源:igfitidea点击:

Value returned by the assignment

javascriptassignment-operator

提问by user10165

Why does the regular assignment statement (say, x = 5) return the value assigned (5in this case), while the assignment combined with a variable declaration (var x = 5) returns undefined?

为什么常规赋值语句(例如, x = 5)返回分配的值(5在这种情况下),而赋值结合变量声明 ( var x = 5) 返回undefined

I got the return values by executing these statements in the Chrome browser's Javascript console:

我通过在 Chrome 浏览器的 Javascript 控制台中执行这些语句来获得返回值:

> var x = 5;
undefined
> y = 5;
5

采纳答案by Paul

That's the way the language was designed. It is consistent with most languages.

这就是语言的设计方式。它与大多数语言一致。

Having a variable declaration return anything other than undefinedis meaningless, because you can't ever use the varkeyword in an expression context.

让变量声明返回任何undefined没有意义的东西,因为你永远不能var在表达式上下文中使用关键字。

Having assignment be an expressionnot a statementis useful when you want to set many variable to the same value at once:

当您想一次将多个变量设置为相同的值时,将赋值设置为expressionnot astatement很有用:

x = y = z = 2;

It can also be used like this:

它也可以这样使用:

x = 2*(y = z); // Set y = z, and x = 2*z

However that is not the most readable code and it would probably be better written as:

然而,这不是最易读的代码,最好写成:

y = z;
x = 2*z;

回答by Ja?ck

That's because var x = 5;is a variable statement, not an expression.

那是因为var x = 5;是变量语句,而不是表达式。

The behaviour of this statement is described in Section 12.2 of the ECMAScript Language Reference.

该语句的行为在 ECMAScript 语言参考的 12.2 节中描述。

  1. Evaluate VariableDeclarationList.
  2. Return (normal, empty, empty).
  1. 评估 VariableDeclarationList。
  2. 返回(正常,空,空)。

This is basically a voidreturn value.

这基本上是一个void返回值。

回答by orb

The assignment operator (i.e., the equals sign) (1) assigns the right-side-operand (i.e., a value or the value of a variable, property, or function) to the left-side-operand (i.e., variable or property) and then (2) the assignment expression (e.g., y = 10) becomes a simple operand with the same value as its right-side-operand (e.g., 10) before the rest of the expression is evaluated. This is similar to when a called function is replaced with its return value when an expression is evaluated (although function calls are first in the order of operations and assignment operations are fourteenth):

赋值运算符(即等号) (1) 将右侧操作数(即变量、属性或函数的值或值)分配给左侧操作数(即变量或属性) ) 然后 (2) 赋值表达式(例如,y = 10)在计算表达式的其余部分之前变成一个简单的操作数,其值与其右侧的操作数(例如,10)相同。这类似于在计算表达式时将被调用函数替换为其返回值(尽管函数调用按操作顺序排在第一位,赋值操作排在第十四位):

var x, y, z = 1;
x = z + (y = 2); // x === 3     

function returnTwo () {
    return 2;
}

x = z + returnTwo(); // x === 3

Take note that not only does x now equal 3, but the entire expression evaluates to 3.

请注意,现在不仅 x 等于 3,而且整个表达式的计算结果为 3。

The purpose of the varkeyword is to bind variables to the current scope. Variables declared with the varkeyword are bound to the scope where they are declared. The varkeyword assigns the left-most variable (or property) as a reference to the value of the evaluated expression:

var关键字的目的是将变量绑定到当前作用域。使用var关键字声明的变量绑定到它们声明的范围内。的var关键字受让人最左边的变量(或属性),作为对所评估的表达式的值的引用:

var fun = function () {
    var x = 1;
    var y = x + 1; 
    return y;
}

// The x and y variables are bound to the scope of the fun function.

Using the varkeyword with an expression is called a declaration. Declarations are actions that do not evaluate to a value, not even undefined (even though your console is printing undefined). Further, declarations cannot appear where JavaScript expects an expression, as other answers to this post have shown.

var关键字与表达式一起使用称为声明。声明是不评估为值的操作,甚至不是未定义的(即使您的控制台打印未定义)。此外,声明不能出现在 JavaScript 需要表达式的地方,正如这篇文章的其他答案所显示的那样。

回答by Sachin

When you write var x = 5;it declares xand initalizes its value to 5.

当您编写var x = 5;它时,它会声明x并将其值初始化为 5。

This is a VariableStatement, it returns nothing,

这是一个VariableStatement,它什么都不返回,

but x=5is an expressionthat assigns 5 to x. as there is no x, JavaScript implicitly creates a global xin normal code

但是x=5expression将 5 分配给 x 的一个。因为没有x,JavaScript 会x在普通代码中隐式地创建一个全局变量

回答by Karol

I edited my answer because of comment and some other answers.

由于评论和其他一些答案,我编辑了我的答案。

Assignment operator doesn't return anything... In below example, first thing JS parser does is assigning 5 to y. Second thing is assigning y to x, and so on. Assigning is not return(it's not a function, and in JS it doesn't have C++ syntax to overload operator's behavior). returnis sth more complex then assignment. returnconstruct is not only returning a value, but is closing current context causing it to be destroyed. Also it's closing any parent context (closure pattern) if there is no other child using it. So please, DO NOT tell me (in comments) that assignment operator returns any value. Assignment operator in case of JS is only a language construct.

赋值运算符不返回任何东西......在下面的例子中,JS 解析器做的第一件事就是将 5 赋值给 y。第二件事是将 y 分配给 x,依此类推。赋值不是return(它不是一个函数,在 JS 中它没有 C++ 语法来重载运算符的行为)。return比赋值更复杂。return构造不仅返回一个值,而且关闭当前上下文导致它被销毁。如果没有其他孩子使用它,它也会关闭任何父上下文(关闭模式)。所以请不要告诉我(在评论中)赋值运算符返回任何值。JS 中的赋值运算符只是一种语言结构。

This language construct is useful in chains (and that's why everyone is talking about returning):

这种语言结构在链中很有用(这就是为什么每个人都在谈论返回):

a = x = y = 5;

Any undeclared variable is declared automatically by parser in global scope. If you declare variable explicitly, then you can't at the same time use it in chain, like this:

任何未声明的变量都由解析器在全局范围内自动声明。如果显式声明变量,则不能同时在链中使用它,如下所示:

a = var x = y = 5;

Proper use of above code would be:

正确使用上述代码将是:

var x = y = 5;
a = x;

Of course you can use brackets, to make your code clearer, but it doesn't mean that code behaves like a function.

当然,您可以使用方括号使您的代码更清晰,但这并不意味着代码的行为就像一个函数。

Also your example works only in JS console, which is not returning but printing the result of statement, or expression. It means that JS console treats result of declaring of variable as undefined(same when creating function: function a() {}).

此外,您的示例仅适用于 JS 控制台,它不返回而是打印语句或表达式的结果。这意味着JS控制台将声明变量的结果视为undefined(创建函数时相同:)function a() {}