0

I am struggling with the this keyword. I am comparing the value of this within a declared function vs within an expression function.

// Declared function
function declaredFunction() { console.log(this); }

// Expression function
var expressionFunction = (() => { console.log(this); }) ;

I define the two of them in a this.js file and run it with a simple node this.js command.

declaredFunction();
expressionFunction();

declaredFunction() logs the global object in the console :

Object [global] {
  global: [Circular],
  clearInterval: [Function: clearInterval],
  clearTimeout: [Function: clearTimeout],
  setInterval: [Function: setInterval],
  setTimeout: [Function: setTimeout] { [Symbol(util.promisify.custom)]: [Function] },
  queueMicrotask: [Function: queueMicrotask],
  clearImmediate: [Function: clearImmediate],
  setImmediate: [Function: setImmediate] {
    [Symbol(util.promisify.custom)]: [Function]
  }
}

expressionFunction() just logs {}.

Two questions :

  1. What is {} (an empty object, obviously, but I don't understand what it is from a semantic point of view); and
  2. Why doesn't my expressionFunction() log the global object too?

There is definitely something I don't understand here about how declaring a function differs from assigning a function expression to a variable.

jonrsharpe
  • 99,167
  • 19
  • 183
  • 334
  • 1
    The difference is between a "traditional" function (using the `function` keyword) and an "arrow" function (using `=>`). Calling the latter type of function does not set the value of `this`. – Pointy Aug 21 '19 at 19:54
  • Note that `var expressionFunction = function () { console.log(this) }` would be a fairer comparison. – jonrsharpe Aug 21 '19 at 20:06
  • According to the JS spec, if there is no explicit `this`, `this` will be the global object (`global` on NodeJS). On the global scope however (and for a reason I don't know / understand) `this` in NodeJS points to `module.exports` (which is an empty object as long as you dont export something). – Jonas Wilms Aug 21 '19 at 20:37

1 Answers1

-1

Currently you are comparing the "standard" way of defining JavaScript functions to the 'newer' arrow functions. You can read more about them here.

From the site:

An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords. Arrow function expressions are ill suited as methods, and they cannot be used as constructors.

To sum it up, this means that while the this keyword in the arrow function prints an object (which is defined in your program's global scope) the standard one would print empty object (which is defined in its own scope).

Personally, I have been using arrow functions increasingly in projects since sometimes you don't want the function to create its own scope but rather this to point to the main scope.

AGoranov
  • 1,023
  • 2
  • 10
  • 25
  • The question is especially about NodeJS, the answer fails to take that into account – Jonas Wilms Aug 21 '19 at 20:38
  • @JonasWilms From Wikipedia Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser. This means that Node.js simply executes JavaScript but outside of browser. Can you please elaborate why is my answer not suitable enough to deserve a downvote? https://en.wikipedia.org/wiki/Node.js – AGoranov Aug 21 '19 at 20:39