I am trying to iterate over all the globals defined in a website, but in doing so I am also getting the native browser functions.
var numf=0; var nump=0; var numo=0;
for(var p in this) {
if(typeof(this[p]) === "function"){
numf+=1;
console.log(p+"()");
} else if(typeof p != 'undefined'){
nump+=1;
console.log(p);
} else {
numo+=1;
console.log(p);
}
}
Is there a way to determine if a function is native to the browser or created in a script?
You can call the inherited
.toString()function on the methods and check the outcome. Native methods will have a block like[native code].Update because a lot of commentators want some clarification and people really have a requirement for such a detection. To make this check really save, we should probably use a line line this:
Now we’re using the
.toStringmethod from theprototypeofFunctionwhich makes it very unlikely if not impossible some other script has overwritten thetoStringmethod. Secondly we’re checking with a regular expression so we can’t get fooled by comments within the function body.