![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Simple overview: I want to call a function in a javascript duntion where the name of the called function is an argument value passed into the javascript function. Here is what I want: On the html pages: page1: onclick="A('a', 'C')" In the library: function A(x, B) { y=...do stuff...; B(y);} function C(x) {...} Currently, I have A call a function D which has a switch statement to call the proper C function. How can I have A call C directly using the passed in name of C? |
#3
| |||
| |||
|
|
sheldonlg wrote: Simple overview: I want to call a function in a javascript duntion where the name of the called function is an argument value passed into the javascript function. Here is what I want: On the html pages: page1: onclick="A('a', 'C')" In the library: function A(x, B) { y=...do stuff...; B(y);} function C(x) {...} Currently, I have A call a function D which has a switch statement to call the proper C function. How can I have A call C directly using the passed in name of C? Functions are first class objects so don't pass a function name in, pass the function itself in e.g. div onclick="A('a', C);" then you can simply use function A(x, F) { y = ...; F(y); } |
#4
| |||
| |||
|
|
You mean simply leave off the quotes in the initial call is all I have to do? |
#5
| |||
| |||
|
|
sheldonlg wrote: You mean simply leave off the quotes in the initial call is all I have to do? Yes, you can do that to pass the function as an argument to the other function. |
#6
| |||
| |||
|
|
In comp.lang.javascript message <48b572c9$0$12953$9b4e6d93 (AT) newsspool2 (DOT) ar cor-online.net>, Wed, 27 Aug 2008 17:29:13, Martin Honnen mahotrash (AT) yahoo (DOT) de> posted: sheldonlg wrote: You mean simply leave off the quotes in the initial call is all I have to do? Yes, you can do that to pass the function as an argument to the other function. For those who really do want to pass in the name of the function as a string (as originally asked), the notation shown by window["LZ"](5) is available; for me, that returns '05'. No doubt someone will comment if that method is not always available. If the function is frequently needed, consider : Fn = window["LZ"] Fn(5) + Fn(6) // gives '0506' If, having received a function itself, one wants the name. one can generally get it by a RegExp match on Fn.toString() ; js-nclds.htm . It's a good idea to read the newsgroup c.l.j and its FAQ. See below. |
#7
| |||
| |||
|
|
For those who really do want to pass in the name of the function as a string (as originally asked), the notation shown by window["LZ"](5) |
![]() |
| Thread Tools | |
| Display Modes | |
| |