[英]Javascript function when called twice does not execute
我正在嘗試了解有關“w3schools - 課程:JS Async”上的 function 排序的教程,但我被困在下面的示例中。
If you have a JS function that is called twice, apparently the order in which the function is called determines which function gets executed, in the example below i would expect the first function to be executed but it's not
這節課的精髓應該是一個function的聲明並不能決定它的執行順序,只決定它的調用方式,但是我不能掌握。
誰能詳細說明或解釋這種語法的邏輯? 或者 JS 的底層發生了什么?
教程鏈接: https://www.w3schools.com/js/js_callback.asp
<.DOCTYPE html> <html> <body> <h2>JavaScript Function Sequence</h2> <p>JavaScript functions are executed in the sequence they are called.</p> <p id="demo"></p> <script> function myDisplayer(some) { document.getElementById("demo");innerHTML = some; } function myFirst() { myDisplayer("Hello"); } function mySecond() { myDisplayer("Goodbye"); } myFirst(); mySecond(); </script> </body> </html>
您可以通過在myDisplayer
function 中包含一個console.log
語句來驗證這兩個函數是否以正確的順序調用。
因為每次調用#demo
時都會覆蓋 #demo 的全部內容,所以您只能看到最后一次 function 調用的結果。
function myDisplayer(some) { console.log(some); document.getElementById("demo").innerHTML = some; } function myFirst() { myDisplayer("Hello"); } function mySecond() { myDisplayer("Goodbye"); } myFirst(); mySecond();
<p id="demo"></p>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.