Home > Web Front-end > JS Tutorial > Javascript implements continuous assignment_javascript skills

Javascript implements continuous assignment_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 15:46:01
Original
1652 people have browsed it

I recently came into contact with the project and had enough time. I went shopping online and accidentally discovered this problem online. The predicted results were very different from the real results.

Please see the code below

var a={n:1}
var b=a;
a.x=a={n:2}
console.log(a.x); 
console.log(b.x);
undefined
Object{n:2}
Copy after login

If we take the code apart and look at it, the result is exactly what we thought

var a={n:1}
var b=a;
a={n:2};
a.x={n:2}
console.log(a.x);//Object{n:2}
console.log(b.x);//undefined
Copy after login

Analysis code:

a.x=a={n:2}

The assignment operation of js is right associative, the above is equivalent to a.x=(a={n:2})

The evaluation operation of js is from left to right (PS: After discussing with a colleague, he said it was from left to right, I thought it was from right to left, but I finally found out that I was wrong)

1) a.x=(final result in brackets)

After finding that the x attribute of a does not exist, add an attribute

2) Because we want to get the result in the brackets, then a.

3) Return to a={n:2} in the brackets, and the direction of a has changed

Javascript implements assigning value to href

The above content is the entire content of this article using javascript to implement continuous assignment. I hope it will be helpful to everyone. This site will be updated with new content every day, so continue to pay attention!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template