18.this指向.html 916 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <div id="box1">12</div>
  10. <script>
  11. var box1 = document.getElementById("box1");
  12. // 点击事件 this指向当前点击对象
  13. box1.onclick = function() {
  14. console.log(this);
  15. }
  16. // 定时器 this指向window
  17. // var timer = setInterval(()=>{
  18. // console.log(this,'定时器')
  19. // },1000)
  20. // 对象中 当前对象
  21. var person = {
  22. name:"孙悟空",
  23. age:18,
  24. say:function() {
  25. console.log(this);
  26. }
  27. }
  28. person.say();
  29. // 函数 this指向window
  30. function fn1() {
  31. console.log(this);
  32. }
  33. fn1()
  34. </script>
  35. </body>
  36. </html>