The Function
class (Figure A-7) provides basic features for functions and it is used throughout the library.
Static method that validates a list of parameters against of list of JSON objects representing the validation rules.
arguments – Array of parameters to be validated.
expectedParams – Array of JSON objects describing the validation rules for the arguments.
Each JSON object contains a validation rule for a parameter. The JSON object is a dictionary of keys and values.
The list of keys that can be used is given in the following table:
Key |
Description |
---|---|
|
The name of the parameter |
|
The allowed type for this parameter (examples: |
|
Boolean value indicating whether this parameter can be passed as |
|
Boolean value indicating whether this parameter is a DOM element or not |
|
Boolean value indicating whether this parameter should have an integer value or not |
|
Boolean value indicating whether this parameter is optional or not |
|
Boolean value indicating whether this parameter should be an |
|
The allowed type for each element of an array (type must be |
|
Boolean value indicating whether an array element could have |
|
Boolean value indicating whether each element of an array is a DOM element (type must be |
|
Boolean value indicating whether each element of an array should have an integer value (type must be |
The function returns an Error
message if the parameters don’t validate and this error is typically thrown as shown below.
if (e) throw e;
This error could be caught and the appropriate measures can be taken programmatically. If not caught, the error will pop up in the debugging console of the browser.
Static method that creates a delegate for a given function and instance object.
instance – the referenced object inside the method.
method – the method for which the delegate is created.
When the handler is an instance method and uses the this
word in its body, we need to attach it as an event handler. We can use this method so that in the returned function this
means the same thing as in the original instance context.
Static method that creates a callback function for a given method and an optional context.
method – the method for which the callback is created.
context – the arbitrary context for calling the callback function.
The context parameter is optional, but if it is omitted the callback function simply represents the original method with an additional level of indirection.
Even if the callback function is called without any parameters, the initial context will still be remembered.