简体   繁体   English

JavaScript中令人困惑的逗号运算符。 为什么以这种方式工作?

[英]Confusing comma operator in JavaScript. Why does it work in such manner?

Could anybody explain why the output of the code 任何人都可以解释为什么代码输出

var a = [1,2];
console.log("Array item: " + a[a=a.toString(), 1]);
console.log("String char: " + a[1]);

looks like this? 看起来像这样吗?

Array item: 2                                                                   
String char: , 

The question is why array didn't convert to string in the first console.log. 问题是为什么数组在第一个console.log中没有转换为字符串。 How do memory and pointers work in this case? 在这种情况下,内存和指针如何工作?

a[a = a.toString(), 1] evaluates a at first, which points to an array yet, then it replaces a with a stringified a which won't affect the already evaluated part, then accesses index 1 of the array. a[a = a.toString(), 1]首先评估a ,它指向一个数组,然后将其替换a不会影响已评估部分的字符串化a ,然后访问该数组的索引1 Its the same as: 它与:

var b = a;
a.toString();
b[1]

Now a[1] evaluates to , because a points to the string now, and therefore it gets the second character. 现在a[1]的计算结果为,因为a指向字符串现在,因此它得到了第二个字符。

Here is how the parser sees it: 解析器如何看待它:

a[a = a.toString(), 1]
// Evaluation of a
[1, 2][a = a.toString(), 1]
// Evaluation of the comma operator, a is turned into a string
[1, 2][1]
// Prop access
2

What you're facing is evaluation order, access order and how the memory is storing the values. 您面临的是评估顺序,访问顺序以及内存如何存储值。

Javascript as other languages evaluates from right to left, Although the comma operator evaluates first a=a.toString() in memory a equals to [1,2] because the most at right value will be evaluated first before modifying the variable a , so a[1] = 2 . 与其他语言一样,Javascript从右到左进行评估,尽管逗号运算符首先评估内存中a a=a.toString()等于[1,2]因为在修改变量a之前,首先评估最右边的值,因此a[1] = 2

After that access, the variable a equals to "1,2" and a[1] = , , in memory a String is like an array of chars and is possible to access using indexes. 访问之后,变量a等于"1,2"a[1] = , ,在内存中,String就像一个char数组,可以使用索引进行访问。

This is how the access occurs: 这是访问发生的方式:

          +------------+-------------+
          | Var a      | Access      |
          +------------+-------------+
       +--|    [1,2]   | a[1] -> 2   |-+---> This is the first access.
Memory |  +--------+-----------------+ |<--- Here the first value no longer exists and now a = String.
       +--|    "1,2"   | a[1] -> "," |-+---> This is the second access.
          +--------+-----------------+

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

相关问题 Javascript:逗号运算符,var和作用域-为什么它按它的方式工作? - Javascript: Comma operator, var, and scope - why does it work the way it does? Javascript。 为什么此代码不起作用? - Javascript. Why does not work this code? HTML和Javascript。 为什么我的代码不起作用 - HTML and Javascript. Why does my code not work JavaScript的。 用户功能方面的承诺。 为什么不起作用? - JavaScript. Promises in User Functions. Why does not it work? 为什么|| 操作员不能在 JavaScript 中工作? - Why does the || operator not work in JavaScript? 为什么三元运算符中的逗号会在JavaScript中引发语法错误? - Why does this comma inside a ternary operator throw a syntax error in JavaScript? Javascript:为什么 % 运算符作用于字符串? - Javascript : Why does % operator work on strings? Javascript:为什么前缀运算符只能与模数一起使用,而不能与后缀运算符一起使用? - Javascript: Why does prefix operator work with modulus but not postfix operator? Javascript函数。 不行 - Function on Javascript. Not work 为什么JavaScript有这么令人困惑的API? - Why does JavaScript have such a confusing API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM