Social Icons

Pages

Thursday, December 1, 2016

Introduction to Objects I 19/33

More Kinds of Methods
Let's look at another method that calculates useful information about an object.
Here we have an object square with asideLength property to represent the length of the square's side. This time, we have added a new method, calcPerimeter, which computes the perimeter of the square. Notice we make use of the keyword return (in the same way we use it in functions!).

Instructions
Add another method called calcArea, which returns the area of square in terms ofsideLength. Use the calcPerimeter function as a guide.
?
Hint
The calcArea method will look a lot like thecalcPerimeter method, except it should return this.sideLength * this.sideLength.


var square = new Object();
square.sideLength = 6;
square.calcPerimeter = function() {
  return this.sideLength * 4;
};
// help us define an area method here
square.calcArea = function() {
  return this.sideLength * this.sideLength;
};

var p = square.calcPerimeter();
var a = square.calcArea();










No comments:

Post a Comment