首页 > 其他 > 详细

this 的默认绑定

时间:2020-06-11 20:39:24      阅读:33      评论:0      收藏:0      [点我收藏+]

今天有一个关于 this 的默认绑定:

    this 的绑定规则  在 非严格模式下,完全取决于函数的调用位。

    默认绑定:this 绑定到全局,即 Window;

   

   例如:

  示例一:

   function foo(){

       console.log(this.a);

   };

   var a = 2;

   foo();   // 2     this 绑定到 Window

  示例二:

   function foo(){

       "use strict";

       console.log(this.a);

   };

   var a = 2;

   foo();   // TypeError: this is undefined;

   在严格模式下,this 绑定到 undefined;

   示例三:

  function a(){

        funtion b(){

           console.log(this);

        };

        b();

   };

   a();   // Window 

 

   这种情况,如果是 绑定到 a 的话,

    

   function a(){

        funtion b(){

           this();

        };

        b();

   };

   a();   // 会造成死循环,所以是错误的;

 

   

   

        

  

 

this 的默认绑定

原文:https://www.cnblogs.com/xhQ2/p/13095405.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!