Here is the code that creates the add variable and passes it to the update function.
var add = function (c, d) {
return c + d;
};
update(add);
That could also be done without creating the add variable, by passing the function directly to the update function.
update(function (c, d) {
return c + d;
});
In both cases, the function that does the adding is an anonymous function.
The anonymous function is created by using a function expression, which is different from the typical functions that you normally deal with which are created by using a [url=“https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/function”]function statement.
Here is a function created using the function statement, and one created using the function expression.
// function statement
function foo(bar) {
...
}
// function expression
var baz = function (bat) {
...
};
When I used the term “object” before, that is only because everything in javascript is an object. The window object and the document object you already know about, but strings, numbers, and functions are also treated as objects.
When I referred to the func object that’s in the update function, it’s was purely to refer to the variable called func, which contains the anonymous function that we created to add numbers.
The reason why the func object contains a function to add numbers, is so that you can pass different functions in to the update function to perform subject, divide, multiply, square roots, x to the power of y, and so on.