Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

let c=(b=a)的意思是啥

网上看到个源码,大意如下:

let a =1;
let b
let c = (b=a)

console.log(a,b,c)

求前辈翻译一下哈


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

let c=b=a=1; 连续赋值语法,每个变量的值都等于最右边的结果

如果加了括号 含义有一点不太一样

let c = (b = a); 可以认为把括号中的结果赋值给c, 问题就变成了 b = a返回的结果是多少。
一个例子

let a;
console.log(a = 1);  // 输出1,注意和  条件判断a == 1区别

再来一个复杂的例子

let a = 1;
let b;
let c = ((b = a) + 1);
console.log(a,b,c); // 输出 1 1 2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...