18: Make Your Own Method



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;


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.


Thank you! I appreciate it!


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