Question #42

Author: admin
tags: JavaScript  
let showX;
let x = 1;

let obj1 = {
  x: 2,
  yyy: function() {
     showX = () => { return this.x; }
   }
};

obj1.yyy();

let obj2 = {
  x: 3,
  z: showX
};

console.log (obj2.z());
What will the console output be?
1
2
3
undefined
Nothing, an error will occur.
The arrow function takes the this value from outside.
This value of the this is immutable for the arrow function.
It is set at the time of creation of the arrow function.
In this case, the arrow function was created when obj1.yyy() was called and its this has reference to obj1.
Rate the difficulty of the question:
easyhard