1. Whether the specified function exists
function isExitsFunction(funcName) {
Try {
If (typeof(eval(funcName)) == "function") {
return true;
}
} catch(e) {}
Return false;
}
2. Similar to PHP’s commonly used judgment function, if it does not exist, create it
if (typeof String.prototype.endsWith != 'function') {
String.prototype.endsWith = function(suffix) {
Return this.indexOf(suffix, this.length - suffix.length) !== -1;
};
}
3. Determine whether the js function exists. If it exists, execute it
Assuming that funcName is the function name, you can achieve the goal by using the following method
Be sure to add a try catch block, otherwise it will not work.
try
{
if(typeof(eval(funcName))=="function")
{
funcName();
}
}catch(e)
{
//alert("not function");
}
4. Whether the specified variable exists
function isExitsVariable(variableName) {
Try {
If (typeof(variableName) == "undefined") {
//alert("value is undefined");
return false;
} else {
//alert("value is true");
return true;
}
} catch(e) {}
Return false;
}