首页 > 代码库 > 箭头函数普通函数this
箭头函数普通函数this
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://google.github.io/traceur-compiler/bin/traceur.js"></script> <script src="https://google.github.io/traceur-compiler/bin/BrowserSystem.js"></script> <script src="https://google.github.io/traceur-compiler/src/bootstrap.js"></script> </head> <script type="text/traceur"> window.onload=function(){ /*var d = {b:1}; d.a = function (){ (() => { console.log(this.b); //1 })(); } d.a(); alert(456); */ b = 2000; var d1 = {b:1}; var d2 = {b:11}; d1.aa = function (f){ f(); //2000 //f.call(d2); //箭头函数即使是call仍然是定义时的window,普通函数用call调用改变this,普通函数在调用处决定this,箭头函数在定义时决定this, (() => { //console.log(this); //d1 })(); } /*d1.aa(() => { console.log(this); //Window,调用相当于是在window中定义的函数,函数定义的时候参数是加入了局部函数作用域 }); */ d1.aa(function(){ console.log(this); //Window }); /*d1.aa((function(){ console.log(this); //Window })());*/ } </script> <body> <div id="app-3"> </div> </body> </html>
箭头函数普通函数this
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。