Question:

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

function Whiteboard() {
  this.purpose = "brainstorm";
  this.businessReason = function () {
    return this.purpose + "…..profit!!"
  }
}
var w = new Whiteboard();
console.log(w.businessReason());

 

Answer:

brainstorm…..profit!!

The businessReason() method uses the "this" keyword to refer to the object that is being created by the constructor function and is assigned to the variable w in this example.


Keywords:

© 2017 QuizBucket.org