Question:

What does the following code print to the console?

function Hat() {
  var brand = "shhh can't tell";
  this.getBrand = function () { return brand; };
}
var my_hat = new Hat();
console.log(my_hat.getBrand());

Answer:

"shhh can't tell"

The brand variable cannot be directly accessed outside of the Hat() function. However, the brand variable can be accessed anywhere within the Hat() constructor function. The getBrand() method can access the brand variable and make it indirectly accessible.


Keywords:

© 2017 QuizBucket.org