DEV Community

Dharan Ganesan
Dharan Ganesan

Posted on

Day 13: Guess it

Today we will see the output based question

1. Closure

function outer() {
  var x = 10;

  function inner() {
    console.log(x);
  }

  x = 20;
  return inner;
}

var closureFunc = outer();
closureFunc(); // output ?
Enter fullscreen mode Exit fullscreen mode

2. This

const obj = {
  count: 0,
  increment() {
    this.count++;
  },
};

const increment = obj.increment;
increment();
console.log(obj.count); // output ?

Enter fullscreen mode Exit fullscreen mode

3. Arrow Function and this

const obj = {
  name: 'Alice',
  age: 30,
  sayHello: function() {
    setTimeout(() => {
      console.log(`Hello, I'm ${this.name} and I'm ${this.age} years old.`);
    }, 1000);
  },
};

obj.sayHello(); // output ?
Enter fullscreen mode Exit fullscreen mode

4. Fn Borrow

const obj = {
  count: 0,
  increment() {
    this.count++;
  },
};

const increment = obj.increment.bind(obj);
increment();
console.log(obj.count);
Enter fullscreen mode Exit fullscreen mode

Write the answer in the comments

Top comments (0)