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
2.
Why is checking for null a good practice?
3.
Checking for null prevents the use of an object before it's initialized.
A.
Checking for null prevents errors resulting in NaN.
B.
Checking for null prevents the need to throw custom errors.
C.
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:
<script>
window.onload = function () {
WillCallBackWhenDone(MyCallBack, 3, 3);
}
function WillCallBackWhenDone(f, a, b) {
var r = a * b;
f(r);
}
function MyCallBack(result) {
alert(result);
}
</script>
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