18: Make Your Own Method


#1


https://www.codecademy.com/en/courses/spencer-sandbox/2/6


Can someone explain to me why I didn't have to type the below codes before calling the method?:

rectangle.setHeight = setHeight; (and) rectangle.setWidth = setWidth

Below, you will see that the method setHeight is associated with rectangle hence the rectangle.setHeight...AND it's within the object's brackets. Yet the rectangle.setWidth is not within the object's bracket. I guess my question is, (since I got this right) WHAT is this exercise trying to teach me?

Here is the entire thing:

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

rectangle.setWidth = function (newWidth) {
  this.width = newWidth;
};
  
rectangle.setHeight(6);
rectangle.setWidth(8);


#3

Because the methods are declared directly on the rectangle object.

You might be thinking of an earlier lesson where we create a bob object and a static function, setAge(). In that lesson we copied the .toString() form of the function into a new method of bob. Not the case here, though. What you have is correct.


#4

Thank you! I appreciate it!


#5

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