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. <button id="btn" onclick="go()">btn</button>
    11. <script>
    12. /*
    13. 怎么理解函数内部this指向
    14. tips: this取声明值,是在函数执行的时候确认,而不是在定义的时候确定
    15. 1 在普通函数中的this指向
    16. */
    17. function go(){
    18. console.log(this);
    19. }
    20. go()
    21. </script>
    22. </body>
    23. </html>
    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. <button id="btn">btn</button>
    11. <script>
    12. var btn =document.getElementById("btn");
    13. btn.onclick =function(){
    14. setTimeout(function(){
    15. console.log(this);
    16. },500)
    17. }
    18. </script>
    19. </body>
    20. </html>
    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. <script>
    11. // 4 call apply bind 函数关键字的指向
    12. function fn1(){
    13. console.log(this);
    14. }
    15. var obj ={
    16. name:"vue"
    17. }
    18. fn1()
    19. fn1.call(obj);
    20. var fn2 =fn1.bind(obj);
    21. fn2();
    22. </script>
    23. </body>
    24. </html>
    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. <script>
    11. // 2 作为对象的方法去调用
    12. // 3 在箭头函数中this指向问题
    13. var name =window
    14. var obj ={
    15. name:"javascript",
    16. sayName(){
    17. console.log(this.name);
    18. },
    19. wait(){
    20. setTimeout(function(){
    21. console.log(this.name);
    22. })
    23. },
    24. delay(){
    25. setTimeout(()=>{
    26. console.log(this.name);
    27. })
    28. },
    29. layOUT(){
    30. setTimeout(function(){
    31. console.log(this.name);
    32. }.bind(this))
    33. },
    34. print(){
    35. var that =this;
    36. setTimeout(function(){
    37. console.log(this.name);
    38. }.bind(that))
    39. }
    40. }
    41. obj.sayName()
    42. obj.wait()
    43. obj.delay()
    44. obj.layOUT()
    45. obj.print()
    46. </script>
    47. </body>
    48. </html>
    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. <!--
    11. 5 class this
    12. class和构造函数中this指向new关键字实例化的对象
    13. -->
    14. <script>
    15. class Person{
    16. skill ="lisi";
    17. constructor(name,age){
    18. this.name =name;
    19. this.age =age;
    20. console.log(this);
    21. }
    22. }
    23. var p =new Person("lisi",18);
    24. console.log(p.name);
    25. </script>
    26. </body>
    27. </html>