| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <button id="btn">点击我</button>
- <script>
- // this 指向问题 this一般指向调用它的对象
- // 如果有事件绑定,this指向事件绑定的对象
- let btn = document.getElementById("btn");
- btn.onclick = function(){
- // function bar(){
- // console.log(this);
- // }
- // bar();
- let bar = () => {
- console.log(this);
- }
- bar();
- console.log(this);
- }
- // 普通函数中 this指向window对象 默认为winow调用
- // function foo(){
- // console.log(this);
- // // 内部函数中 this指向window对象
- // function bar(){
- // console.log(this);
- // }
- // bar();
- // }
- // foo();
- // let obj = {
- // name:"张三",
- // age:18,
- // sex:"男",
- // sayName(){
- // console.log(this);
- // // function bar(){
- // // console.log(this);
- // // }
- // // bar();
- // // 箭头函数中 this指向为当前所在作用域的this (箭头函数中没有this)
- // let bar = () => {
- // console.log(this);
- // }
- // bar();
- // }
- // }
- // obj.sayName();
- </script>
- </body>
- </html>
|