#Callback Exercises
-
Write a function,
funcCaller
, that takes afunc
(a function) and anarg
(any data type). The function returns thefunc
called witharg
(as an argument). -
Write a function,
firstVal
, that takes an array,arr
, and a function,func
, and callsfunc
with the first index of thearr
, the index # and the whole array. -
Change
firstVal
to work not only with arrays but also objects. Since objects are not ordered, you can use any key-value pair on the object. -
[Extra Credit] Write a function,
once
, (see: http://underscorejs.org/#once) that takes a function and returns a version of that function which can only be called once. [Hint: you need a closure] You probably don't want to be able to double charge someone's credit card. Here is an example of how to use it:
var chargeCreditCard = function(num, price){
//charges credit card for a certain price
};
var processPaymentOnce = once(chargeCreditCard);
processPaymentOnce(123456789012, 200);