1. <button id="btn">btn</button>
    2. <script>
    3. /*
    4. 情况:
    5. 1.普通函数
    6. 2.使用call、apply、bind
    7. 3.class中
    8. 4.箭头函数
    9. 5.事件中(谁调用指向谁)
    10. 6。作为对象的调用的方法
    11. */
    12. var btn = document.getElementById("btn")
    13. btn.onclick = function () {
    14. console.log(this); //这里是普通事件,谁调用指向谁
    15. setTimeout(function () {
    16. console.log(this); //这里是普通函数,普通函数的调用指向的是windows
    17. })
    18. setTimeout(() => {
    19. console.log(this); //箭头函数this指向上下文。
    20. })
    21. }
    22. class Student {
    23. constructor(name, age) {
    24. this.name = name;
    25. this.age = age;
    26. };
    27. say() {
    28. console.log(this);
    29. return this
    30. }
    31. }
    32. var s = new Student("cheng", 18)
    33. console.log(s.age);
    34. console.log(s.say);
    35. s.say()
    36. </script>