跳至內容

eslint/no-alert 限制

功能

禁止使用 alert、confirm 和 prompt

為何這不好?

JavaScript 的 alert、confirm 和 prompt 函式被廣泛認為是突兀的 UI 元素,應替換為更合適的自訂 UI 實作。此外,alert 通常在除錯程式碼時使用,應在部署到生產環境之前移除。

範例

此規則的不正確程式碼範例

js
alert("here!");

confirm("Are you sure?");

prompt("What's your name?", "John Doe");

此規則的正確程式碼範例

js
customAlert("Something happened!");

customConfirm("Are you sure?");

customPrompt("Who are you?");

function foo() {
  var alert = myCustomLib.customAlert;
  alert();
}

參考資料

以 MIT 授權發布。