« Back to the index

Function.prototype.buildFilter example

The "filter" method allow the programmer to define if the main function will be executed or not:
newfunction = function.buildFilter(filterfuntion);
The "filterfunction" is a function that accept the same parameters of the main function and will return true if the main function can be executed, or false if not.
This function is very usefull to verify the validity of the parameters and execute the main function only if anything is correct, or execute the main function only if any special condition is met.

Let's use a basic function that calculates the difference between 2 values:
function test(var1, var2) { return var2-var1; }
View the result of this code
The filter function allow the execution only if the second parameter is greater than the first parameter.
The result returned by the main function will always be undefined if the filter does not allow the execution.
var filtertest = test.buildFilter( function(v1, v2) { return v2 >= v1; } );
View the result of this code


« Back to the index