Getting a JS function by name

Discuss your code, get questions answered

Getting a JS function by name

Postby oliver » Tue Jul 24, 2012 7:01 am

As explained in the JavaScript API doc, MorphEngine has objects like this

calculator.functions
calculator.functions.complex
calculator.functions.vector
etc.

that contain the actual JS functions for commands. These objects are called "function collections" but are really ordinary JS objects that happen to contain Functions as properties.

An alias ME is available that is simply a shortcut to calculator.functions.

So, a function like sin would be found in calculator.functions.sin (or ME.sin) and calculator.functions.complex.sin (ME.complex.sin), etc.
In JS, the dot-notation is just syntactic sugar equivalent to the following: calculator.functions["sin"] (ME["sin"]), and calculator.functions.complex["sin"] (ME.complex["sin"]).

As a convenience when you write JS programs, you can actually write just
Code: Select all
sin(x)
to invoke calculator.functions.sin (ME.sin), although you can also write calculator.functions.sin(x) or ME.sin(x).
To call the complex version, though, you have to either write
Code: Select all
complex.sin(x)
(Which works because–and this is more internal/technical than you probably care for–all identifiers are scoped to look in ME, via a JS with (ME) instructions.)
or
Code: Select all
ME.complex.sin(x)

The same goes for functions defined in your folder (be they JavaScript or RPL). If you have a function called foo in your folder, you can invoke it from JavaScript like so:
Code: Select all
foo(x)


Now, if you need a handle to the actual function and look it up by name (to dynamically get access to a JS function), here's how you can do that:
Let's assume the name of the function you want to call is in a variable name.
For a stock function with real data type: calculator.functions[name] or ME[name]
For a function in the current folder: calculator.functions[calculator.currentDataCategory] or ME[calculator.currentDataCategory]
For a function in another folder: calculator.functions[foldername] or ME[foldername] (where foldername is something like "Tools")

Once you have the function, you can invoke it with JavaScript's call or apply functions.

Examples:

Call a real-valued function by name:
Code: Select all
function(name, val) {
    var f = ME[name];
    return f.call(ME, val);
}

or
Code: Select all
function(name, val) {
    var f = ME[name];
    var args = [];
    args.push(val);
    return f.apply(ME, args);
}


Call a function (JavaScript *or* RPL) by name, defined in the current folder:
Code: Select all
function(name, val) {
    var f = ME[calculator.currentDataCategory][name];
    return f.call(ME[calculator.currentDataCategory], val);
}

or
Code: Select all
function(name, val) {
    var f = ME[calculator.currentDataCategory][name];
    var args = [];
    args.push(val);
    return f.apply(ME[calculator.currentDataCategory], args);
}
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Return to Programming

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron