| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- #box {
- width: 300px;
- height: 300px;
- background: #f00;
- }
- </style>
- </head>
- <body>
- <div id="box"></div>
- <script>
- let box = document.getElementById("box");
- // 1.点击事件 this指向当前对象
- // box.onclick = function() {
- // console.log(this);
- // }
- // 2.定时器 this指向window
- // setInterval
- // setTimeout(() => {
- // console.log(this)
- // },3000)
- // box.onclick = function () {
- // setInterval(function(){
- // console.log(this)
- // }, 1000)
- // }
- // 3. 对象中
- /**
- * this指向
- * 普通函数 指向调用本身
- * 箭头函数 指向上下文
- */
- // let obj = {
- // aa:1,
- // bb:2,
- // cc:()=> {
- // console.log(this);
- // }
- // }
- // obj.cc();
- // 4.普通函数 this指向window
- function fn1() {
- console.log(this)
- }
- fn1();
- </script>
- </body>
- </html>
|