[英]Which is right way to use below statement performance wise?
最推薦哪種方法。 第一還是第二?
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();
}
如果發現x.length > 0
那么您肯定會得到x[0]
並且不需要檢查&& x[0].blur();
。 因此,第二種方法要好得多。
if (x.length > 0) {
x[0].blur();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.