window.alert() MDN

在VSCode中,練習TypeScript語法時,使用套件Code Runner 在VSCode中OUTPUT輸出結果。發現alert函式出現錯誤。

//陳述式 DECLARATION
function printMessage(message: string):void {
  alert('message')
}
printMessage('january')

小複習 TypeScript:
message: string 參數 型別註釋(Type Annotation)
:void 函式不會return,使用時機: 函式內做console.log、alert、修改節點

上方函式在VSCode的OUTPUT中出現以下錯誤:

ReferenceError: alert is not defined
    at printMessage (c:\Users\Mi Jou Hsieh\typescript\hello.js:96:5)
    at Object.<anonymous> (c:\Users\Mi Jou Hsieh\typescript\hello.js:99:1)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

CHATGTP解說

這個錯誤訊息說明了在程式執行時 alert 函式未被定義。
alert 是瀏覽器中用來顯示訊息框的函式,但它並不是 JavaScript 語言本身的一部分
因此 在一些環境中(例如 Node.js)中執行時可能會報錯

若想在瀏覽器環境中使用 alert,請確保你在一個支援瀏覽器 API 的環境下執行你的程式,
比如瀏覽器的 JavaScript 運行環境。如果你是在 Node.js 或其他非瀏覽器環境中執行,
可以考慮使用其他方式來顯示訊息,比如使用 console.log 來輸出至控制台。


#alert() #Web APIs #alert不是JS語法 #alert 需在browser中執行







Related Posts

變成rule的形狀(2) - ESLint

變成rule的形狀(2) - ESLint

GIT Github

GIT Github

[Day06] Applicative

[Day06] Applicative


Comments