0 votes
in JavaScript by
What is the output?
var status = '';

setTimeout(() => {
  const status = '';

  const data = {
    status: '',
    getStatus() {
      return this.status;
    },
  };

  console.log(data.getStatus());
  console.log(data.getStatus.call(this));
}, 0);
  • A: "" and ""
  • B: "" and ""
  • C: "" and ""
  • D: "" and ""

1 Answer

0 votes
by

Answer: B

The value of the this keyword is dependent on where you use it. In a method, like the getStatus method, the this keyword refers to the object that the method belongs to. The method belongs to the data object, so this refers to the data object. When we log this.status, the status property on the data object gets logged, which is "".

With the call method, we can change the object to which the this keyword refers. In functions, the this keyword refers to the the object that the function belongs to. We declared the setTimeout function on the global object, so within the setTimeout function, the this keyword refers to the global object. On the global object, there is a variable called status with the value of "". When logging this.status"" gets logged.

Related questions

0 votes
asked Mar 15 in JavaScript by DavidAnderson
0 votes
asked Mar 13 in JavaScript by DavidAnderson
...