it makes no sense to use it immediately. you must use it to create a function to attach to an event or use as a callback for an asynchronous function. an example might be like this:
function factory(param){ return function(result) { if (result==param) dosomething(); } } $('#domobject').click({ param = $('#domvalue').value; asynch_function(factory(param)); });
Here I added a click event, presumably a button. When it is pressed, it will receive the input value and create a function on it and will call the asynchronous function with the newly created function as a callback. An asynchronous function may be an ajax request. When the asynchronous function finishes executing the function created by the factory, a callback will be called. It will check the return value that the asynchronous function passed to the callback with respect to the parameter specified when the event was connected.
If we move the dom search inside the callback function, then we do not need a factory or param, but then it will use the value that is in the input when the asynch function returns, and than when the button was pressed later, and the value could change .
Sometimes you won’t be able to get the value you need in the callback context for other reasons. Or it may just be that you want to abstract away from the function class, so you do not need to re-enter a slightly different version in all places where you use it.
source share