4.修改this指向的方法.html 547 B

12345678910111213141516171819202122232425262728
  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. let obj = {
  11. name: "张三",
  12. }
  13. function fn(x, y) {
  14. const sum = x + y;
  15. console.log(sum);
  16. console.log(this.name);
  17. console.log(this);
  18. }
  19. fn.bind(obj, 2, 3)()
  20. // fn.apply(obj, [2, 3])
  21. // fn.call(obj, 2, 3)
  22. </script>
  23. </body>
  24. </html>