You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Logical OR assignmenta||=b;// 等价于 a || (a = b);// Logical AND assignment a&&=b;// 等价于 a && (a = b);// Logical nullish assignmenta??=b;// 等价于 a ?? (a = b);
// Display a default message if it doesn’t override anything.// Only assigns to innerHTML if it’s empty. Doesn’t cause inner// elements of msgElement to lose focus.functionsetDefaultMessage(){msgElement.innerHTML||='<p>No messages<p>';}// Display a default message if it doesn’t override anything.// Buggy! May cause inner elements of msgElement to// lose focus every time it’s called.functionsetDefaultMessageBuggy(){msgElement.innerHTML=msgElement.innerHTML||'<p>No messages<p>';}
目前 js 支持 3 种逻辑赋值运算符:
与数学意义的逻辑运算符不同的是,逻辑赋值符在不符合逻辑条件下不会触发 setter
实际使用中,逻辑赋值运算符能提供更好的性能,如下面代码:
上述代码中赋值给
.innerHTML
属性会删除msgElement
元素内部子元素,然后插入从新分配的字符串后再进行解析,而逻辑赋值运算符正好能解决这种不必要的副作用。目前 chrome 86 已经支持,
但最新版 node v14.13.1 还未支持nodejs v15.0.0开始支持。The text was updated successfully, but these errors were encountered: