我认为这样的事情应该有效:
假设你有这样的事情:
var functions = {
aFunctionRun: function () {return false;}
bFunctionRun: function ('foobar') {}
cFunctionRun: function () {}
}
试试这个代码:
function isEmpty(function){
// Get the string from the function;
var funcString = function.toString();
// Cut off the part before the actual content of the function
funcString = funcString.substr(funcString .indexOf(')'))
.replace(/function|[(){};]/g,'') // remove the function keyword, and the following characters: (){};
.trim(); // remove any leading / trailing whitespaces and
return funcString === ''; //check if it's an empty string.
}
isEmpty(functions.aFunctionRun); // returns false
isEmpty(functions.bFunctionRun); // returns true
isEmpty(functions.cFunctionRun); // returns true