How to change what an ES6 arrow function's 'this' points to?

允我心安 提交于 2019-12-02 01:21:11

How do you change this in ES6 arrow functions?

You can't, that's the whole point of using arrow functions (besides concise syntax). If you need to control this, don't use an arrow function.

Arrow functions in ES6 have lexical this. This means you can always tell their this-value by asking the question "What was the value of this when the function was defined?" They are immune to the this-altering effects of call, apply, bind, and method invocation.

Let's take a look at the intended effect by compiling ES6 to ES5 with Babel.

ES6:

let foo = {
  bar() {
    return [
      () => this,
      function() { 
        return this 
      }
    ]
  },
  baz: () => this
}

let fns = foo.bar()
fns.push(...fns.map((fn, i) => fn.bind({i})))
fns.map(fn => fn()).forEach(thisVal => {
  console.log(thisVal)
  console.log('-------')
})
console.log(foo.baz())

Compiled to ES5:

'use strict';

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var foo = {
  bar: function bar() {
    var _this = this;

    return [function () {
      return _this;
    }, function () {
      return this;
    }];
  },

  baz: function baz() {
    return undefined;
  }
};

var fns = foo.bar();
fns.push.apply(fns, _toConsumableArray(fns.map(function (fn, i) {
  return fn.bind({ i: i });
})));
fns.map(function (fn) {
  return fn();
}).forEach(function (thisVal) {
  console.log(thisVal);
  console.log('-------');
});
console.log(foo.baz());

Check out what happened inside foo.bar. There was an inserted line:

var _this = this;

The arrow function's body was changed so that it returned _this from its lexical outer scope. Therefore, binding the value of this is not supposed to have any effect. By "freezing" the value of "this" just before the function definition, we have prevented the ability to change the function's behavior through binding, using call/apply, or calling the function as a method.

Notice how baz's this value was simply replaced with undefined. That's because if we ask the question "What was the value of this when the function was defined?", we obviously must answer either undefined or some global object context, like Node's global or the browser's window. By asking this question, you can easily deduce the value of this inside arrow functions, and that is a big part of their inherent value.

The output is therefore:

{ bar: [Function: bar], baz: [Function: baz] }
-------
undefined
-------
{ bar: [Function: bar], baz: [Function: baz] }
-------
{ i: 1 }
-------
undefined

The first and third outputs correspond to the arrow function defined within bar(). Notice how they automatically have a this-value that points to the object referred to by foo because they were created with the foo.bar() call. Useful right? Only the behavior of the non-arrow function could be changed by binding. Before binding, it's this-value was undefined, a common cause of many JS bugs and redundant bindings when passing around functions. Arrow functions take away a lot of that hassle.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!