运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,暗/亮
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript "this"</h1> <p>This example demonstrate that in Arrow Functions, the "this" keyword represents the object that owns the function, no matter who calls the function.</p> <p>Click the button to execute the "hello" function again, and you will see that "this" still represents the window object.</p> <button id="btn">Click Me!</button> <p id="demo"></p> <script> let hello = ""; hello = () => { document.getElementById("demo").innerHTML += this; } //The window object calls the function: window.addEventListener("load", hello); //A button object calls the function: document.getElementById("btn").addEventListener("click", hello); </script> </body> </html>