Why using 'this' in this exercise?


#1

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


var rectangle = new Object();
rectangle.height = 3;
rectangle.width = 4;
// here is our method to set the height
rectangle.setHeight = function (newHeight) {
  this.height = newHeight;
};
// help by finishing this method
rectangle.setWidth = function (newWidth) {
    this.width = newWidth;
};
  rectangle.setHeight(6);
rectangle.setWidth(8);
// here change the width to 8 and height to 6 using our new methods


So if it is restricted to only one object, why do we use "this" in this exercise in the first place?


#2

this is an important keyword and has many implications.

In object's method, it simply points to the object that it was called on.

Here you will find lots of examples.


#3

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