Javascript a=b=c 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7511279/
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
Javascript a=b=c statements
提问by amit
I searched through the internets but could not find a relevant search criteria so I thought this would be the best place to ask.
我在互联网上搜索,但找不到相关的搜索条件,所以我认为这是最好的提问地点。
I have a JS statement saying
我有一个 JS 声明说
document.location.hash = this.slug = this.sliceHashFromHref(href)
How does this work??
这是如何运作的??
采纳答案by Pranay Rana
It gets evaluted from right to left. i.e.
它从右到左进行评估。IE
document.location.hash = this.slug = this.sliceHashFromHref(href)
means the output/value of this.sliceHashFromHref(href)
is assigned to this.slug
and then to document.location.hash
.
表示将 的输出/值this.sliceHashFromHref(href)
分配给this.slug
,然后分配给document.location.hash
。
回答by aioobe
How does this work??
这是如何运作的??
a = b
can be seen as both a statementand an expression.
a = b
可以看作是一个语句和一个表达式。
The result of the expression is b
.
表达式的结果是b
。
In other words,
换句话说,
a = b = c;
which can be written as
可以写成
a = (b = c);
is equivalent to
相当于
b = c;
a = b;
Thus your code is equivalent to:
因此,您的代码等效于:
this.slug = this.sliceHashFromHref(href);
document.location.hash = this.slug;
回答by Ma Jerez
Be aware of the variables scope!!
注意变量作用域!!
var A = B = C = 3; //A is local variable while B & C are global variables;
var A = 3 , B = 3, C = 3;// A B C are local variables;
回答by Stefan Gehrig
Quite easy... It assigns the result from the call to this.sliceHashFromHref(href)
to both document.location.hash
and this.slug
, so both properties (variables) contain the same value after the line has been executed.
很简单……它将调用结果分配this.sliceHashFromHref(href)
给两者document.location.hash
和this.slug
,因此在执行该行后,两个属性(变量)都包含相同的值。
回答by GordonM
In Javascript (and several other languages that derive their syntax from C) an assignment evaluates the item to the right of the = symbol and assigns it to the variable on the left. The item on the right can itself be an assignment with an = operator. What happens is the rightmost expression is evaluated, the value assigned to the middle variable, and then that value is assigned to the variable on the left.
在 Javascript(以及其他几种从 C 派生语法的语言)中,赋值计算 = 符号右侧的项并将其分配给左侧的变量。右侧的项目本身可以是带有 = 运算符的赋值。发生的事情是评估最右边的表达式,将值分配给中间的变量,然后将该值分配给左边的变量。
In short, it's simply a way to assign a value to multiple variables at once.
简而言之,它只是一种同时为多个变量赋值的方法。
回答by Martian2049
Actually Ma Jerez's answer makes a very important point here. also this answer refers to this similar question: this question involves a few things:
实际上马赫雷斯的回答在这里提出了一个非常重要的观点。这个答案也指的是这个类似的问题:这个问题涉及一些事情:
- hoist: variables are hoisted before block code execution;
=
assignment order: it goes from right to left;- global context: in non-strict mode, when a variable isn't defined, it goes to the global context; but will throw in 'use strict' mode;
- 提升:在块代码执行之前提升变量;
=
赋值顺序:从右到左;- 全局上下文:在非严格模式下,当一个变量没有被定义时,它会进入全局上下文;但会进入“使用严格”模式;
example1:
示例1:
;(function Anonymous(){
var a = b = {};
console.log(a==b); //true
})();
a
was hoisted in theAnonymous
execution scope.b
is going to be assigned as{}
, but becauseb
is not defined,b
is assigned to the global contextwindow
, thenwindow.b
is assigned{}
; thenwindow.b = {}
return
s{}
.local variable
a
is assigned as {}.
a
被提升到Anonymous
执行范围。b
将被赋值为{}
,但因为b
没有定义,b
被赋值给全局上下文window
,然后window.b
被赋值{}
;然后window.b = {}
return
s{}
。局部变量
a
被赋值为 {}。
Therefore, a few interesting things happen here: the local variable a
and the global variable b
both point to the same object {}
, so they are ==
and ===
; remember that {}=={}
gives false
otherwise.
因此,这里发生了一些有趣的事情: thelocal variable a
和 theglobal variable b
都指向同一个 object {}
,所以它们是==
and ===
;请记住,否则{}=={}
给出false
。
Note: if in strict mode:
注意:如果在严格模式下:
;(function Anonymous(){
'use strict'
var a = b = {}; //Uncaught ReferenceError: b is not defined
})();
this consecutive assignment won't work the same way...
这个连续的任务不会以同样的方式工作......