Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS 运算符优先级及其应用 #37

Open
songning0605 opened this issue Sep 5, 2020 · 0 comments
Open

JS 运算符优先级及其应用 #37

songning0605 opened this issue Sep 5, 2020 · 0 comments
Labels

Comments

@songning0605
Copy link
Owner

运算符优先级见:MDN

看一个例子

var a = {n:1};  
var b = a; // 持有a,以回查  
a.x = a = {n:2};  
alert(a.x);// --> undefined  
alert(b.x);// --> {n:2}

解题步骤:

这一题要考的是运算符优先级的问题,. 的运算优先级高于 =

  1. 首先执行点运算符,在对象a中声明属性x,此时b指向a,a,b中都有一个未赋值的属性x
  2. 接下来执行=赋值操作,a 首先被重新指向 {n: 2}
  3. 赋值运算符返回 {n: 2} 重新赋值给步骤1中创建的 x 所指向的内存地址,由于 a 已经被重定向,所以对x的赋值操作只会影响b,而不会影响a

所以结果为:

a = {n:2};
b = {n:1, x: {n: 2}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant