Exercises
- Create an object called
shape
that has the typeproperty
and agetType()
method. - Define a
Triangle()
constructor function whose prototype isshape
. Objects created withTriangle()
should have three own properties—a
,b
, andc
, representing the lengths of the sides of a triangle. - Add a new method to the prototype called
getPerimeter()
. - Test your implementation with the following code:
> var t = new Triangle(1, 2, 3); > t.constructor === Triangle; true > shape.isPrototypeOf(t); true > t.getPerimeter(); 6 > t.getType(); "triangle"
- Loop over
t
showing only own properties and methods (none of the prototype's). - Make the following code work:
> [1, 2, 3, 4, 5, 6, 7, 8, 9].shuffle(); [2, 4, 1, 8, 9, 6, 5, 3, 7]
Get Object-Oriented JavaScript - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.