简体   繁体   中英

What is the order precedence of a = b == c in JavaScript?

var clicked = $(event.currentTarget || target);
var clickedIsActive = clicked[0] == this.active[0];

I'm fairly new to js, and while attempting to read through some jQuery code, I came across the above section.

What is the precedence for the second line?

Is it:

var clickedIsActive = (clicked[0] == this.active[0]);

Or is it something else?

Thank you.

Yes, the rightmost side of an assignment is evaluated first.

clickedIsActive is assigned the result of the expression clicked[0] == this.active[0] .

var clickedIsActive = clicked[0] == this.active[0];

clickedIsActive是比较clicked[0] == this.active[0]的结果,所以clicked[0] == this.active[0]必须先进行比较。

I think you might be confusing the = with the == . They're not the same thing so this is very much like comparing apples with oranges.

= is an assignment. == is a "is equal" comparison that will only return true or false .

Misunderstanding or not, your transcode is correct. The right side of an assignment is parsed before the actual assignment. The Javascript VM needs to know what it's assigning something as before it can save it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM