Question:

What does the following code print to the console?

function Raisin() {
  this.formerSelf = "grape";
  this.goodness = function (number) {
    return "I am " + number + " times worse than when I was a " + this.formerSelf
  };
}
var r = new Raisin();
console.log(r.goodness(10));

Answer:

"I am 10 times worse than when I was a grape"

When functions are invoked as constructors, the "this" keyword refers to the object that is being created. In this example, we are creating an object that is assigned to the r variable. The "this" keyword in the goodness() function refers to the object that is assigned to the r variable.


Keywords:

© 2017 QuizBucket.org