123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <!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>
- <div id="box">hello world</div>
- <script>
- var oBox = document.getElementById("box");
- // oBox.onclick = function(){
- // console.log(this.innerText);
- // }
- // oBox.onclick = function(){
- // console.log(this);
- // (function(){
- // console.log(this)
- // })()
- // }
- // 箭头函数内没有this概念 但是里面可以使用this this指向的是外层的this
- // oBox.onclick = function(){
- // console.log(this);
- // (()=>{
- // console.log(this)
- // })()
- // }
- // var obj = {
- // a:10,
- // foo:function(){
- // console.log(this.a);
- // }
- // }
- // var a = 20;
- // var obj = {
- // a:10,
- // foo:function(){
- // console.log(this.a);
- // (function(){
- // console.log(this.a)
- // })()
- // }
- // }
- // obj.foo();
- // var a = 10;
- // function foo2(){
- // var a = 1
- // console.log(this.a);
- // }
- // window.foo2();
- // this 指向 : 指向调用当前函数的对象 要么指向window
- // 对象当中方法默认指向当前对象 如果方法内有其他函数 ,其他函数内部的this指向window
- // 普通函数this指向window
- </script>
- </body>
- </html>
|