首页 > 其他 > 详细

区别ES3ES5和ES6this的指向问题。区分普通函数和箭头函数中this的指向问题

时间:2018-03-10 19:53:47      阅读:261      评论:0      收藏:0      [点我收藏+]
  • ES3 ES5this的指向问题 this指的是该函数被调用的对象
      var foo = function () {
        this.a = ‘a‘,
          this.b = ‘b‘,
          this.c = {
            a: ‘new a‘,
            b: function () {
              //new a 此时this指的是该函数被调用的对象
              return this.a;
            }
          }
      }
      console.log(new foo().c.b()); //new a
  • ES6的箭头函数 箭头函数的this指的是定义时this的指向,b在定义时,this指向的是c被定义时的函数
    var foo = function () {
        this.a = ‘a‘,
          this.b = ‘b‘,
          this.c = {
            a: ‘new a‘,
            b: () => {
              //a 箭头函数的this指的是定义时this的指向,b在定义时,this指向的是c被定义时的函数,
              return this.a;
            }
          }
      }
      console.log(new foo().c.b()); //a 

区别ES3ES5和ES6this的指向问题。区分普通函数和箭头函数中this的指向问题

原文:https://www.cnblogs.com/luxiaoyao/p/8541625.html

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