| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <!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-color: #f00;
- }
- </style>
- </head>
- <body>
- <div id="box"></div>
- <script>
- const box = document.getElementById("box");
- // 1.点击事件 指向当前对象
- // box.onclick = function () {
- // console.log(this);
- // }
- // // 2.定时器 指向window
- // setInterval(() => {
- // console.log(this);
- // }, 2000);
- // 3.对象中 普通函数方法 this指向当前对象
- // 3.对象中 箭头函数方法 this指向window
- // let obj = {
- // name: "张三",
- // age: 18,
- // say: () => {
- // console.log(this);
- // }
- // }
- // obj.say();
- // 4.普通函数 this指向window
- function fn() {
- console.log(this);
- }
- fn()
- </script>
- </body>
- </html>
|