HTML and CSS Reference
In-Depth Information
Which of the following isn't a property of the exception object?
A. message
B. description
C. number
D. name
Why is checking for null a good practice?
Checking for null prevents the use of an object before it's initialized.
Checking for null prevents errors resulting in NaN.
Checking for null prevents the need to throw custom errors.
Objective 2.4: Implement a callback
Callbacks are a design pattern to implement when you are working with multiple threads or
just needing to have something work asynchronously. The concept of the callback is quite
straightforward and is used throughout this topic quite heavily. The idea of a callback is to
call a function to run but when it's done, to call back a specified function with usually some
sort of result or status of the operation. The “Using advanced array methods” section earlier
in this chapter demonstrates a few of the functions available on the array object that take a
callback as a parameter. The general pattern is shown here:
window.onload = function () {
WillCallBackWhenDone(MyCallBack, 3, 3);
function WillCallBackWhenDone(f, a, b) {
var r = a * b;
function MyCallBack(result) {
In this code example, two functions are declared: WillCallBackWhenDone and MyCallBack .
One parameter to the WillCallBackWhenDone function is a function followed by two other
variables, which in this case are numbers that will be multiplied. The product of the multi-
plication is passed to the callback function. This case is a bit over the top for the usage of
callbacks, but it does demonstrate the pattern involved. Anytime a function is called that ex-
pects a function as a parameter, this is what it's doing. Knowing what parameters the callback
function will receive is important so that they can be specified in the parameter list.
Search WWH ::

Custom Search