Questions
Letâs say you want to create a new
Number
method,triple
, which triples the currentNumber
objectâ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
return
statement?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
opacity
differences?Create a custom object with three public methodsâ
changeState
,getColor
, andgetState
âand two private data members,background
andstate
. Set the data members toon
for state, and set a color of#fff
for background color. ThechangeState
method will test to see if the state ison
, and if it is, change it tooff
, and the color to#000
. ThegetColor
method returns the color, and thegetState
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.