74

I've read in several places that the key difference is that this is lexically bound in arrow functions. That's all well and good, but I don't actually know what that means.

I know it means it's unique within the confines of the braces defining the function's body, but I couldn't actually tell you the output of the following code, because I have no idea what this is referring to, unless it's referring to the fat arrow function itself....which doesn't seem useful.

var testFunction = () => {
  console.log(this)
};
testFunction();
Nexo
  • 2,125
  • 2
  • 10
  • 20
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
  • 6
    It simply captures the value of `this` from the containing scope, treating it like any other variable. – Barmar Feb 06 '15 at 18:08
  • 1
    It's just so you don't have to do the kludge of `var self = this;` and then use `self` in the function. – Barmar Feb 06 '15 at 18:09
  • 5
    In your case, there is no enclosing context, or it's the global context, or module context, so `this` is whatever it is in that case, most likely null or window. To put it another way, `this` has exactly the same value as it would if you added a `console.log(this)` before the function assignment. –  Feb 06 '15 at 18:18

10 Answers10

46

Arrow functions capture the this value of the enclosing context

function Person(){
  this.age = 0;

  setInterval(() => {
    this.age++; // |this| properly refers to the person object
  }, 1000);
}

var p = new Person();

So, to directly answer your question, this inside your arrow function would have the same value as it did right before the arrow function was assigned.

Dave
  • 10,748
  • 3
  • 43
  • 54
  • 4
    @torazaburo belated response -- the answer is it depends where that code snippet in the original question was placed. If it was at the top level, `this` is the `window` object if we're in a browser and `module.exports` if we're in a Node environment. The point is, the arrow function *has no effect* on the value of `this`. – temporary_user_name May 18 '17 at 18:33
  • 4
    The comment from @dave, '`this` inside your arrow function would have the same value as it did right before the arrow function was assigned' is what finally made it clicked for me. – Kevin Nov 12 '17 at 07:41
24

In order to provide the big picture I'm going to explain both, dynamic and lexical binding.

Dynamic Name Binding

this refers to the object the method is called on. This is a regularly to be read sentence on SO. But it is still only a phrase, pretty abstract. Is there a corresponding code pattern to this sentence?

Yes there is:

const o = {
  m() { console.log(this) }
}

// the important patterns: applying methods

o.m(); // logs o
o["m"](); // logs o

m is a method because it relies on this. o.m() or o["m"]() means m is applied to o. These patterns are the Javascript translation to our famous phrase.

There is another important code pattern that you should pay attention to:

"use strict";

const o = {
  m() { console.log(this) }
}

// m is passed to f as a callback
function f(m) { m() }

// another important pattern: passing methods

f(o.m); // logs undefined
f(o["m"]); // logs undefined

It is very similar to the previous pattern, only the parenthesis are missing. But the consequences are considerable: When you pass m to the function f, you pull outm of its object/context o. It is uprooted now and this refers to nothing (strict mode assumed).

Lexical (or Static) Name Binding

Arrow functions don't have their own this/super/arguments binding. They inherit them from their parent lexical scope:

const toString = Object.prototype.toString;

const o = {
  foo: () => console.log("window", toString.call(this)),
      
  bar() {
    const baz = () => console.log("o", toString.call(this));
    baz();
  }
}

o.foo() // logs window [object Window]
o.bar() // logs o [object Object]

Apart from the global scope (Window in browsers) only functions are able to form a scope in Javascript (and {} blocks in ES2015). When the o.foo arrow function is called there is no surrounding function from which baz could inherit its this. Consequently it captures the this binding of the global scope which is bound to the Window object.

When baz is invoked by o.bar, the arrow function is surrounded by o.bar (o.bar forms its parent lexical scope) and can inherit o.bar's this binding. o.bar was called on o and thus its this is bound to o.

  • Could you please help me, why without the strict mode, it logs window, but by using strict mode, it logs undefined? Which property of strict mode does this? – Reza Feb 14 '21 at 19:50
9

Hope this code show could give you clearer idea. Basically, 'this' in arrow function is the current context version of 'this'. See the code:

// 'this' in normal function & arrow function
var this1 = {
    number: 123,
    logFunction: function () { console.log(this); },
    logArrow: () => console.log(this)
};
this1.logFunction(); // Object { number: 123}
this1.logArrow(); // Window 
Xin
  • 33,823
  • 14
  • 84
  • 85
  • 3
    Very brief and good example. When using `function` the `this` value is created at time of invoking/calling the function. Therefore, when you call it as `this1.logFunction()` you are calling it as a method of object `this1` and `this` refers to this1 literal object. On the other hand, if you use arrow function, `this` value is not created depending on how is invoked/called but inherits it from the lexical scope, which in this case is `window`object, where this1 obejct is defined. – Barleby Mar 13 '19 at 11:41
2

Arrow function this is pointing to the surrounding parent in Es6, means it doesn't scope like anonymous functions in ES5...

It's very useful way to avoid assigning var self to this which is widely used in ES5...

Look at the example below, assigning a function inside an object:

var checkThis = {
  normalFunction: function () { console.log(this); },
  arrowFunction: () => console.log(this)
};

checkThis.normalFunction(); //Object {}
checkThis.arrowFunction(); //Window {external: Object, chrome: Object, document: document, tmpDebug: "", j: 0…}
Alireza
  • 100,211
  • 27
  • 269
  • 172
1

You can try to understand it by following the way below

// whatever here it is, function or fat arrow or literally object declare
// in short, a pair of curly braces should be appeared here, eg:
function f() {
  // the 'this' here is the 'this' in fat arrow function below, they are
  // bind together right here
  // if 'this' is meaningful here, eg. this === awesomeObject is true
  console.log(this) // [object awesomeObject]
  let a = (...param) => {
    // 'this is meaningful here too.
    console.log(this) // [object awesomeObject]
}

so 'this' in fat arrow function is not bound, means you can not make anything bind to 'this' here, .apply won't, .call won't, .bind won't. 'this' in fat arrow function is bound when you write down the code text in your text editor. 'this' in fat arrow function is literally meaningful here. What your code write here in text editor is what your app run there in repl. What 'this' bound in fat arror will never change unless you change it in text editor. Sorry for my pool English...

Plasmatium
  • 620
  • 6
  • 7
1

Arrow function never binds with this keyword

var env = "globalOutside";
var checkThis = {env: "insideNewObject", arrowFunc: () => {
console.log("environment: ", this.env);
} }

checkThis.arrowFunc()   // expected answer is environment: globalOutside

// Now General function 
var env = "globalOutside";
var checkThis = {env: "insideNewObject", generalFunc: function() {
console.log("environment: ", this.env);
} }
checkThis.generalFunc() // expected answer is enviroment: insideNewObject

// Hence proving that arrow function never binds with 'this'
TrickOrTreat
  • 821
  • 1
  • 9
  • 23
1

this will always refer to the global object when used inside an arrow function. Use the regular function declaration to refer to the local object. Also, you can use the object name as the context (object.method, not this.method) for it to refer to the local object instead of the global(window).

0

In another example, if you click the age button below

<script>
var person = {
    firstName: 'John',
    surname: 'Jones',
    dob: new Date('1990-01-01'),
    isMarried: false,
    age: function() {
        return new Date().getFullYear() - this.dob.getFullYear();
    }
};

var person2 = {
    firstName: 'John',
    surname: 'Jones',
    dob: new Date('1990-01-01'),
    isMarried: false,
    age: () => {
        return new Date().getFullYear() - this.dob.getFullYear();
    }
};

</script>



<input type=button onClick="alert(person2.age());" value="Age">

it will throw an exception like this

×JavaScript error: Uncaught TypeError: Cannot read property 'getFullYear' of undefined on line 18

But if you change person2's this line

return new Date().getFullYear() - this.dob.getFullYear(); 

to

return new Date().getFullYear() - person2.dob.getFullYear();

it will work because this scope has changed in person2

Hamit YILDIRIM
  • 4,224
  • 1
  • 32
  • 35
0

Differences between arrow functions to regular functions: (taken from w3schools)

With arrow functions there are no binding of this.

In regular functions the this keyword represented the object that called the function, which could be the window, the document, a button or whatever.

With arrow functions the this keyword always represents the object that defined the arrow function.

// Regular Function:
hello = function() {
  document.getElementById("demo").innerHTML += this;
}

// The window object calls the function:
window.addEventListener("load", hello);

// A button object calls the function:
document.getElementById("btn").addEventListener("click", hello);

// -------------------------------------------

// Arrow function
hello2 = () => {
  document.getElementById("demo2").innerHTML += this;
}

// The window object calls the function:
window.addEventListener("load", hello2);

// A button object calls the function:
document.getElementById("btn2").addEventListener("click", hello2);
<p><i>With a regular function this represents the <b>object that calls the function</b>:</i></p>

<button id='btn'>click me regular function</button>

<p id="demo">Regular function: </p>

<hr>

<p><i>With arrow function this represents the <b>owner of the function(=the window object)</b>:</i></p>

<button id='btn2'>click me arrow function</button>

<p id="demo2">Arrow function: </p>
Ben.S
  • 708
  • 1
  • 5
  • 24
0

A related issue:

Came from - Why can't I access `this` within an arrow function?

We know below from here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Does not have its own bindings to this or super, and should not be used as methods.

Arrow functions establish "this" based on the scope the Arrow function is defined within.

Had a issue with this using arrow functions, so created a class (can be function), and class variable is accessed in arrow function, thereby achieved smaller functions using arrow functions without function keyword:

class MyClassOrFunction {
    values = [];
    size = () => this.values.length;
    isEmpty = () => this.size() === 0;
}

let obj = new MyClassOrFunction();
obj.size(); // function call here

You can also have a getter like this, that does not have function keyword, but a bit longer due to return statement, also can access other member functions:

class MyClassOrFunction {
    values = [];
    size = () => this.values.length;
    get length() {  return this.size();  }
}
let obj = new MyClassOrFunction();
obj.length; // NOTE: no function call here
Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140