-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay5Ex1.js
35 lines (30 loc) · 1.09 KB
/
Day5Ex1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*Write a constructor called Rectangle that accepts two numbers
(width and height) as parameters.
Rectangle instances should have a method called getArea that
returns the instance's width multiplied by its height.
Write another constructor called Square that accepts one number
(which will serve as both width and the height) as a parameter.
Instances of Square should also have a getArea method
but you should not rewrite the getArea function you wrote for Rectangle.
Square instances should use the same getArea method that Rectangle instances do.
var square = new Square(4);
square.getArea(); //16
var rect = new Rectangle(4, 5);
rect.getArea(); //20 */
//------------------------------------------------------------------------
function Rectangle(width, height) {
this.width = width;
this.height = height;
}
Rectangle.prototype.getArea = function() {
return this.width * this.height;
};
function Square(side) {
this.width = side;
this.height = side;
}
Square.prototype = new Rectangle();
var square = new Square(4);
square.getArea(); //16
var rect = new Rectangle(4, 5);
rect.getArea(); //20