Questions

  1. Let’s say you want to create a new Number method, triple, which triples the current Number object’s value. You also want this method available for all numbers. What are the steps you’d take?

  2. How do you hide a data member with a new object? Why would you want to?

  3. Create a function that wants a number argument and returns an error if the argument is the incorrect type. How would you implement this without having to use the return statement?

  4. We’ve seen object detection used previously with events:

  5.  var theEvent = nsEvent ? nsEvent : window.event;
  6. Why can’t we use the same type of functionality when dealing with the opacity differences?

  7. Create a custom object with three public methods—changeState, getColor, and getState—and two private data members, background and state. Set the data members to on for state, and set a color of #fff for background color. The changeState method will test to see if the state is on, and if it is, change it to off, and the color to #000. The getColor method returns the color, and the getState returns the state.

Answers are provided in the appendix.

Get Learning JavaScript 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.