习题讲解_this指向.html 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. <script>
  10. //1、写出下列输出结果
  11. // 全局变量才会放到window对象中
  12. // var x = 10;
  13. // function test() {
  14. // var x = 20
  15. // console.log(this.x)
  16. // }
  17. // test()
  18. //2、写出下列输出结果
  19. // var name = "window"
  20. // var obj = {
  21. // name: "obj",
  22. // func1: function () {
  23. // console.log(this.name);//obj
  24. // (function () {
  25. // console.log(this.name);//window
  26. // })()
  27. // }
  28. // }
  29. // obj.func1()
  30. //3、写出下列结果
  31. // var name = "the window";
  32. // var object = {
  33. // name: "My Object",
  34. // getName: function () {
  35. // return this.name;
  36. // }
  37. // }
  38. // console.log(object.getName());//My Object
  39. // console.log((object.getName)());//My Object
  40. // console.log((object.getName = object.getName)());//the Window
  41. // let a = 10;
  42. // if(a=3){
  43. // console.log(a);
  44. // }
  45. //4、下列代码中当div的点击事件触发时输出的结果是?
  46. // document.getElementById("div").onclick = function () {
  47. // console.log(this);//div
  48. // };
  49. //5、请写出下列代码运行结果
  50. var name = "window"
  51. var obj = {
  52. name: "obj"
  53. }
  54. setInterval(function () {
  55. console.log(this.name)
  56. }, 300)
  57. setInterval(function () {
  58. console.log(this.name)
  59. }.call(obj), 300)
  60. </script>
  61. </body>
  62. </html>