How to check if function exists in JavaScript?
Try something like this:
if (typeof me.onChange !== "undefined") {
// safe to use the function
}
or better yet (as per UpTheCreek upvoted comment)
if (typeof me.onChange === "function") {
// safe to use the function
}
How to check if function exist?
problem is solved. its works:
if ($.fn.payment) {
//do something
}
How to check if function exists?
You could declare the function as:
declare var functionName: Function | undefined;
How to check if global function exists in JS?
Found the answer:
if (typeof GlobalFunctions != "undefined")
how to check if a function exists
function sb_save(){
alert('saving');
}
$('button').on('click', function(){
if (typeof sb_save==="function"){
sb_save();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>Click</button>
Check if a function exists inside another function in nodejs
Unless you specifically KNOW that this is a global function (which is almost never the case in nodejs), functions by default in nodejs are scoped to the module and there is NO way to look them up by string name like you did with the window
object in the browser, just like there is no way to look up local variables by name inside a function in Javascript.
In general, don't pass functions by string name. Or, if you have to, then you need to create a lookup table that you can check the function name against.
I'd suggest you explain the real problem you're trying to solve here because passing the function by string name is not how you would generally want to do things.
There is a bit of a hack using eval()
that can see if a string represents a function name that is in scope:
// Warning, you must know that the argument f (if it is a string) does not
// contain harmful Javascript code because it will be used with eval()
function isFunction(f) {
// if already a function reference
if (typeof f === "function") {
return true;
// see if string represents a function name somewhere in scope
} else if (typeof f === "string") {
try {
return eval(`typeof ${f} === "function"`);
} catch(e) {
return false;
}
} else {
return false;
}
}
Note: This tests to see if the function is in the scope of the isFunction()
function. If you want to test if it's in your current scope, then you need to do the:
eval(`typeof ${f} === "function"`)
inline in your current scope so it runs in the scope you want to do the lookup from.
To ever consider using this, you will HAVE to know that the source of your string is safe and cannot contain harmful code. But, as I said earlier, it's better to design your program differently so you aren't referring to functions by their string name.
And, here's a runnable snippet that shows it in action (also works in a node.js module):
function test() { console.log("in test");}
function isFunction(f) { // if already a function reference if (typeof f === "function") { return true; // see if string represents a function name somewhere in scope } else if (typeof f === "string") { try { return eval(`typeof ${f} === "function"`); } catch(e) { return false; } } else { return false; }}
console.log(isFunction("test")); // trueconsole.log(isFunction(test)); // trueconsole.log(isFunction("notAFunction")); // false
Related Topics
Has Anyone Else Run into Problems Styling Twitter Typeahead's Searchbar
Reading JavaScript Variable into Shiny/R on App Load
In iOS8 Using .Focus() Will Show Virtual Keyboard and Scroll Page After Touch
Es6 Arrow Functions Not Working on the Prototype
Encrypt iOS and Decrypt Node.Js Aes
How to Convert Special Utf-8 Chars to Their Iso-8859-1 Equivalent Using JavaScript
What's the Difference Between Console.Dir and Console.Log
This' Different Between Repl and Script
Pdf.Js: Rendering a PDF File Using a Base64 File Source Instead of Url
Javascript:Send JSON Object with Ajax
Differences Between Ruby 1.9 and JavaScript Regexp
Es6 Template Literals VS. Concatenated Strings
How to Implement Jquery.Noconflict()