Question:

What does the following code print to the console?

hat = function () {
  return this;
}

obj1 = {
  check: hat
}

obj2 = {
  check: hat
}

console.log(obj1 === obj1.check())
console.log(obj2 === obj2.check())

Answer:

true
true

When the hat() method is bound to obj1, "this" refers to obj1. When the hat() method is bound to obj2, "this" refers to obj2. The "this" keyword for a given method can be assigned to different objects depending on the invocation context for the method.


Keywords:

© 2017 QuizBucket.org