19_改变this指向.html 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. </head>
  9. <body>
  10. <div id="div1"></div>
  11. <script>
  12. /*
  13. 区别:
  14. 1.调用方式不一样
  15. 2.call apply直接调用修改后的方法 bind返回的是一个方法 需要重新调用
  16. call(修改的this,参数1,参数2)
  17. apply(修改的this,【参数1,参数2】)
  18. bind(修改this,参数1,参数2)()
  19. */
  20. // var person = {
  21. // name:'zs',
  22. // age:18,
  23. // eat:function(){
  24. // console.log(this)
  25. // }
  26. // }
  27. // // person.eat()
  28. var person2 = {
  29. name:'lisi',
  30. age:20
  31. }
  32. // person.eat.call(person2)
  33. function xx(x,y){
  34. console.log(x,y,this)
  35. }
  36. xx(1,2)
  37. xx.call(person2,1,2)
  38. xx.apply(person2,[1,2])
  39. //bind不会调用函数 返回的是一个修改this指向之后的方法
  40. xx.bind(person2,1,2)()
  41. </script>
  42. </body>
  43. </html>