hoyong

导航

普通匿名函数作为参数时, 其中的this指向window

<!DOCTYPE html>
<html lang="en">
  <head>
  </head>
 
  <body>
    <script>
    function test( a, b ){
        a+=1;
        b(a);
    }
 
  test(3, function(result){
        console.log('普通匿名函数作为参数时, 其中的this指向window');
        console.log(this);
        console.log(result);
    });
 
    </script>
  </body>
</html>

 

下面的这种函数,并不指向windows,而是事件对应的dom对象:

<div id="a">按</div>
<script>
document.getElementById('a').onclick = function(){alert(this.innerHTML)};
</script>

不过,这样的函数已经不是真正的匿名函数了,比如你可以通过:

document.getElementById('a').onclick();

 

posted on 2019-06-29 12:54  hoyong  阅读(408)  评论(0编辑  收藏  举报