util-ex
Version:
Browser-friendly enhanced util fully compatible with standard node.js
23 lines (22 loc) • 1.41 kB
TypeScript
/**
* Creates a new function with the given name, arguments, and body, and optionally with the given scope and values.
*
* @param {string} name - The name of the function.
* @param {string|string[]=} [aArgs] - An array of argument names. or function body if it's string.
* @param {string} [body] - The function body as a string.
* @param {(string[]|Object)} [scope] - An array of scope names, or an object with scope names as keys and their corresponding values as values.
* @param {Array} [values] - An array of values corresponding to the scope names, if scope is an array.
* @returns {Function} - The newly created function.
*
* @example
*
* // Create a function with no arguments, with scope 'Math' and values [Math]:
* var f = createFunction('myFunc', 'return Math.PI', ['Math'], [Math]);
* var f = createFunction('myFunc', 'return Math.PI', {Math: Math});
*
* // Create a function with two arguments, with scope 'Math' and values [Math], and scope 'console' and values [console]:
* var g = createFunction('myFunc', ['a', 'b'], 'console.log(a + b);', ['Math', 'console'], [Math, console]);
* var g = createFunction('myFunc', ['a', 'b'], 'console.log(a + b);', {Math, console});
*/
export function createFunction(name: string, aArgs?: (string | string[]) | undefined, body?: string, scope?: (string[] | any), values?: any[], ...args: any[]): Function;
export default createFunction;