How to find if a function is inbuilt function or user-defined-Collection of common programming errors
One, perhaps naive, approach would be to test whether the function name exists a property of the document:
function newFunction (){
return true;
}
console.log('newFunction' in document, 'toString' in document);
This is, of course, not exhaustively tested and does fail if the function is created as an extension of a prototype
, for example:
function newFunction (){
return true;
}
Object.prototype.newFunctionName = function () {
return 10 * 2;
};
console.log('newFunction' in document, 'toString' in document, 'newFunctionName' in document); // false, true, true
JS Fiddle demo.
Given that it also fails for 'eval' in document
(in that it returns false
), then this will, or can, only work to identify prototype-methods of the Objects. Which is, at best, an incomplete solution.