JavaScript:this指向问题 - 普通函数、箭头函数、普通函数里面的箭头函数 - 附带案例

x33g5p2x  于2022-02-28 转载在 JavaScript  
字(3.3k)|赞(0)|评价(0)|浏览(795)

this指向

参考文章: https://segmentfault.com/a/1190000011194676

口号1. 普通function函数:谁调用this就是谁 == 可以受call、apply、bind改变this指向2. 箭头函数=>:箭头函数最外层爹是谁,箭头函数里面的this就是谁 == 不受call、apply、bind改变this指向

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="G:\VsCode\开源\jquery-3.5.1\jquery-3.5.1.min.js"></script>
  9. </head>
  10. <body>
  11. </body>
  12. <script>
  13. var meal = "window对象的meal"
  14. const dinner = {
  15. meal: 'dinner对象的meal',
  16. test: function (a, b) {
  17. console.log(this, '==', this.meal, '==', a, '==', b);
  18. },
  19. test2: (a, b) => {
  20. //此箭头函数最外层即dinner,dinner的爹是谁即是window
  21. console.log(this, '==', this.meal, '==', a, '==', b);
  22. },
  23. test3: function (a, b) {
  24. return (a,b) => {
  25. //此箭头函数最外层即是test3,由test3被谁调用确定这里面的this是谁
  26. console.log(this, '==', this.meal, '==', a, '==', b);
  27. }
  28. }
  29. }
  30. console.log("===========================================================")
  31. console.log("===========普通函数 谁调用this就指向谁============")
  32. console.log("===========隐式指定this指向============")
  33. dinner.test(1, 2);
  34. var fun = dinner.test;
  35. fun(1, 2);
  36. console.log("===========call显示指定this指向(函数调用)============")
  37. fun.call(window, 1, 2)
  38. fun.call(dinner, 1, 2)
  39. console.log("===========apply显示指定this指向(函数调用)============")
  40. fun.apply(window, [1, 2])
  41. fun.apply(dinner, [1, 2])
  42. console.log("===========bind强制指定this指向且返回新的函数对象============")
  43. var fun3 = fun.bind(dinner)
  44. fun3(1, 2);
  45. var fun2 = fun.bind(window)
  46. fun2(1, 2);
  47. console.log("===========================================================")
  48. console.log("===========箭头函数 this仅指向最外层的作用域,不受call、bind、apply影响 == obj的爹是window============")
  49. console.log("===========隐式指定this指向============")
  50. dinner.test2(1, 2);
  51. var fun = dinner.test2;
  52. fun(1, 2);
  53. console.log("===========call显示指定this指向(函数调用)============")
  54. fun.call(window, 1, 2)
  55. fun.call(dinner, 1, 2)
  56. console.log("===========apply显示指定this指向(函数调用)============")
  57. fun.apply(window, [1, 2])
  58. fun.apply(dinner, [1, 2])
  59. console.log("===========bind强制指定this指向且返回新的函数对象============")
  60. var fun3 = fun.bind(dinner)
  61. fun3(1, 2);
  62. var fun2 = fun.bind(window)
  63. fun2(1, 2);
  64. console.log("===========================================================")
  65. console.log("===========普通函数里面的箭头函数 this仅指向最外层的作用域,不受call、bind、apply影响============")
  66. console.log("===========隐式指定this指向============")
  67. dinner.test3(1, 2)();
  68. var fun = dinner.test3(); //由于dinner调用,故dinner.test3里面的箭头函数的爹是dinner 【这句等价于下面】
  69. // var fun = dinner.test3.call(dinner)
  70. fun(1, 2);
  71. var funWindow = dinner.test3;
  72. funWindow = funWindow(); //由于window调用,故dinner.test3里面的箭头函数的爹是window 【这句等价于下面】
  73. // funWindow = dinner.test3.call(window)
  74. funWindow(1, 2);
  75. console.log("===========call显示指定this指向(函数调用)============")
  76. fun.call(window, 1, 2)
  77. fun.call(dinner, 1, 2)
  78. funWindow.call(dinner,1,2)
  79. funWindow.call(window,1,2)
  80. console.log("===========apply显示指定this指向(函数调用)============")
  81. fun.apply(window, [1, 2])
  82. fun.apply(dinner, [1, 2])
  83. funWindow.apply(dinner,[1,2])
  84. funWindow.apply(window,[1,2])
  85. console.log("===========bind强制指定this指向且返回新的函数对象============")
  86. var fun3 = fun.bind(dinner)
  87. fun3(1, 2);
  88. var fun2 = fun.bind(window)
  89. fun2(1, 2);
  90. var fun4 = funWindow.bind(dinner)
  91. fun4(1, 2);
  92. var fun5 = funWindow.bind(window)
  93. fun5(1, 2);
  94. </script>
  95. </html>

相关文章