Why would you use "this" in the given example?


#1


https://www.codecademy.com/en/courses/spencer-sandbox/2/6?curriculum_id=506324b3a7dffd00020bf661


Everything works out well, I was just wondering why you would use "this" in the given example? Is there someone with an explanation?


var rectangle = new Object();
rectangle.height = 3;
rectangle.width = 4;
rectangle.setHeight = function (newHeight) {
  this.height = newHeight;
};


#2

Does this refer to something that you want to manipulate?
What is the purpose of the function?
What does the function need to do? What data does it need to access? Where can the function get access to that data?


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.