Javascript a=b=c statements

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

document.location.hash = this.slug = this.sliceHashFromHref(href)

How does this work??


Its get evaluted from right to left. ie

document.location.hash = this.slug = this.sliceHashFromHref(href)

output/value of

this.sliceHashFromHref(href)

is get assigned to document.location.hash = this.slug


How does this work??

a = b can be seen as both a statement and an expression .

The result of the expression is 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;

注意变量范围!

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;
链接地址: http://www.djcxy.com/p/69980.html

上一篇: 在JavaScript中,链接分配好吗?

下一篇: Javascript a = b = c语句