2.this指向.js 596 B

12345678910111213141516171819202122232425262728293031323334
  1. var box = document.getElementById("box");
  2. // 点击事件 当前元素中引用谁 this就指向谁
  3. // box.onclick = function() {
  4. // console.log(this)
  5. // }
  6. // 在定时器中 this指向全局变量 window
  7. // box.onclick = function() {
  8. // setInterval(function(){
  9. // console.log(this);
  10. // },1000)
  11. // }
  12. // 在对象中 this指向当前对象的本身
  13. // var obj = {
  14. // name: 'LiLi',
  15. // sex: '女',
  16. // age: function() {
  17. // console.log(this);
  18. // }
  19. // }
  20. // obj.age();
  21. // 在函数里 this指向window
  22. function fn1() {
  23. console.log(this);
  24. }
  25. fn1();