Sobes.tech
Junior — Senior

Setting the correct context in the setTimeout function

livecode

Task condition

It is necessary to modify the implementation of the method delayedGetName1 so that the call to this.getName() inside it correctly retrieves the object's name and outputs the string "EPAM".

(() => {
  const obj = {
    name: "EPAM",
    getName() {
      return this.name;
    },
    delayedGetName1() {
      setTimeout(function () {
        console.log("1:", this.getName());
      }, 1000);
    },
    delayedGetName2() {
      setTimeout(() => {
        console.log("2:", this.getName());
      }, 1000);
    }
  };

  obj.delayedGetName1();
  obj.delayedGetName2();
})();