![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Say I have this foo = function() { // blah }; bar = fuction(fnName) { /* If fnName equalled "foo" * How do I test that foo, as a function, * exists and then, if it exists, call it? */ |
|
// I tried testing with this if (typeof window[fnName] != 'undefined') { |
#3
| |||
| |||
|
|
On Aug 11, 12:19 pm, Andrew Poulos <ap_p... (AT) hotmail (DOT) com> wrote: Say I have this foo = function() { // blah }; bar = fuction(fnName) { /* If fnName equalled "foo" * How do I test that foo, as a function, * exists and then, if it exists, call it? */ Are you REALLY passing the function by name? Most people would pass it by reference. // I tried testing with this if (typeof window[fnName] != 'undefined') { Assuming you're passing it by reference (its unthinkable otherwise) have you tried: if (fn && fn instanceof Function) { fn(); } |
#4
| |||
| |||
|
|
slebetman wrote: On Aug 11, 12:19 pm, Andrew Poulos <ap_p... (AT) hotmail (DOT) com> wrote: Say I have this foo = function() { * *// blah }; bar = fuction(fnName) { * */* If fnName equalled "foo" * * * How do I test that foo, as a function, * * * exists and then, if it exists, call it? * * */ Are you REALLY passing the function by name? Most people would pass it by reference. * *// I tried testing with this * *if (typeof window[fnName] != 'undefined') { Assuming you're passing it by reference (its unthinkable otherwise) have you tried: * if (fn && fn instanceof Function) { * * fn(); * } Adapted your example for function name strings (because Andrew was very clear about wanting to pass the function name as a string in his example): if(window[fn] && window[fn] instanceof Function){ * *return window[fn](); } |
|
I've had to do this exact thing, but I'm not worried about the risk of variables existing with the same name (due to some extremely unique function names) so I just do this: if(window[fn]) return window[fn](); |
#5
| |||||
| |||||
|
|
On Aug 11, 3:34 pm, Stevo <n... (AT) mail (DOT) invalid> wrote: slebetman wrote: On Aug 11, 12:19 pm, Andrew Poulos <ap_p... (AT) hotmail (DOT) com> wrote: |
|
There is a long and detailed thread about this: URL: http://groups.google.com.au/group/co...aa 7cb290ec3c |
|
A javscript function can be defined as an object that implements an internal [[Call]] method, i.e. that it can be called. The difficulty is that you can't directly test for that other than actually attempting to call it. While it is required that native functions return true for: typeof functionName === 'function' |
|
there is no such requirement for host objects that can be called. For example: alert(typeof document.getElementById); shows "function" in Firefox and "object" in IE, similarly: alert( document.getElementById instanceof Function); returns true in Firefox and false in IE. So testing with either instanceof or typeof is only suitable if the object to be tested is known to be a native object. |
|
-- Rob |
#6
| |||||||||
| |||||||||
|
|
RobG wrote: On Aug 11, 3:34 pm, Stevo <n... (AT) mail (DOT) invalid> wrote: slebetman wrote: On Aug 11, 12:19 pm, Andrew Poulos <ap_p... (AT) hotmail (DOT) com> wrote: There is a long and detailed thread about this: URL: http://groups.google.com.au/group/co...aa 7cb290ec3c I was almost going to recommend the jQuery 'isFunction' function here, as a joke. e.g. This problem has already been solved. See jQuery.isFunction. |
|
A javscript function can be defined as an object that implements an internal [[Call]] method, i.e. that it can be called. The difficulty is that you can't directly test for that other than actually attempting to call it. While it is required that native functions return true for: typeof functionName === 'function' Just because something is callable doesn't mean it is a function. |
|
"is a Function" can be roughly translated to Function.prototype is on the object's prototype chain. |
|
To determine that, instanceof and isPrototypeOf could be used. |
|
An object that implements [[Call]] isn't necessarily a Function. |
|
That's what David spent many long replies trying to explain to Thomas, who seemed to not understand the intent of David's isFunction. |
|
The purpose of knowing if something is a function, rather than is callable, seems to be to know if you can call 'call' or 'apply' directly on that object. |
|
Fortunately, isFunction isn't necessary and typeof is really all you need (see below). |
|
there is no such requirement for host objects that can be called. For example: alert(typeof document.getElementById); shows "function" in Firefox and "object" in IE, similarly: alert( document.getElementById instanceof Function); returns true in Firefox and false in IE. So testing with either instanceof or typeof is only suitable if the object to be tested is known to be a native object. There is no way to determine if a Host object is callable, other than to try and call it, though that may result in Error. |
#7
| |||
| |||
|
|
dhtml wrote: RobG wrote: A javscript function can be defined as an object that implements an internal [[Call]] method, i.e. that it can be called. The difficulty is that you can't directly test for that other than actually attempting to call it. While it is required that native functions return true for: typeof functionName === 'function' Just because something is callable doesn't mean it is a function. And that was not what was said. However, the opposite is likely to be true. |
#8
| |||
| |||
|
|
Adapted your example for function name strings (because Andrew was very clear about wanting to pass the function name as a string in his example): if(window[fn] && window[fn] instanceof Function){ return window[fn](); } |
![]() |
| Thread Tools | |
| Display Modes | |
| |