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
483 views
in Technique[技术] by (71.8m points)

关于变量声明问题。

var s = str || "default"; 
var l = len || s.length; 
var o = option || "i";
请问这么声明变量什么意思呢?谢谢。

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

1 Reply

0 votes
by (71.8m points)

这是短路语法,它比条件语句更加清晰

    let str = a || b;

若 a 为 true,则 str = a

若 a 为 false,则 str = b

    let str = a && b;

若 a 为 true,则 str = b

若 a 为 false,则 str = a

理解

比如 a && b ,a 如果是 true,那么此时结果只跟 b 有关,相当于结果就是 b


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

...