Skip to Content
JavaScript : Object-Oriented Programming
book

JavaScript : Object-Oriented Programming

by Kumar Chetan Sharma et al
August 2016
Beginner to intermediate
847 pages
17h 28m
English
Packt Publishing
Content preview from JavaScript : Object-Oriented Programming

Exercises

  1. Create an object called shape that has the type property and a getType() method.
  2. Define a Triangle() constructor function whose prototype is shape. Objects created with Triangle() should have three own properties—a, b, and c, representing the lengths of the sides of a triangle.
  3. Add a new method to the prototype called getPerimeter().
  4. 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"
    
  5. Loop over t showing only own properties and methods (none of the prototype's).
  6. Make the following code work:
    > [1, 2, 3, 4, 5, 6, 7, 8, 9].shuffle();
           
    [2, 4, 1, 8, 9, 6, 5, 3, 7]
    
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

JavaScript: Functional Programming for JavaScript Developers

JavaScript: Functional Programming for JavaScript Developers

Ved Antani, Simon Timms, Dan Mantyla
Mastering JavaScript Object-Oriented Programming

Mastering JavaScript Object-Oriented Programming

Andrea Chiarelli, Lyubomyr Rudko

Publisher Resources

ISBN: 9781787123595Supplemental Content