Question:

What does the following code print to the console? Explain the use of the "this" keyword.

var honeyBadger = {
  personality: "vicious",
  modusOperandi: function () {
    return "I am " + this.personality + " because I don't care!";
  }
}
console.log(honeyBadger.modusOperandi());

Answer:

"I am vicious because I don't care!"

Notice that the "this" keyword in the modusOperandi() method refers to the honeyBadger object. When functions are invoked as methods, "this" refers to the object that defines the method.


Keywords:

© 2017 QuizBucket.org