Questions
Let’s say you want to create a new
Numbermethod,triple, which triples the currentNumberobject’s value. You also want this method available for all numbers. What are the steps you’d take?How do you hide a data member with a new object? Why would you want to?
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
returnstatement?We’ve seen object detection used previously with events:
var theEvent = nsEvent ? nsEvent : window.event;
Why can’t we use the same type of functionality when dealing with the
opacitydifferences?Create a custom object with three public methods—
changeState,getColor, andgetState—and two private data members,backgroundandstate. Set the data members toonfor state, and set a color of#ffffor background color. ThechangeStatemethod will test to see if the state ison, and if it is, change it tooff, and the color to#000. ThegetColormethod returns the color, and thegetStatereturns the state.
Answers are provided in the appendix.
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.
Read now
Unlock full access