[英]Which is right way to use below statement performance wise?
Which approach is most recommended. 最推荐哪种方法。 First or Second ?
第一还是第二?
var x = document.querySelectorAll("span");
// #1: Console returns false
x.length > 0 && x[0].blur();
// #2: Console returns undefined
if (x.length > 0) {
x[0].blur();
}
If you find x.length > 0
then you will definitely get x[0]
and do not need to check && x[0].blur();
如果发现
x.length > 0
那么您肯定会得到x[0]
并且不需要检查&& x[0].blur();
. 。 So your second approach is much better.
因此,第二种方法要好得多。
if (x.length > 0) {
x[0].blur();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.