如何取消設定 JavaScript 變數?


若要取消設定 JavaScript 中的變數,請使用 undefined。然後,使用 delete 運算子完全刪除它。

讓我們再次嘗試程式碼片段。

var str = “Demo Text”;
window.onscroll = function() {
   var y = this.pageYOffset;
   if(y > 200) {
      document.write(nxt);
      nxt = undefined; // unset
     delete(nxt); // this removes the variable completely
     document.write(nxt); // the result will be undefined
   }
}

更新於:2019 年 9 月 13 日

17K+ 次瀏覽

啟動你的職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.