Is [object Function] unique to built-in functions?
I am trying to write a bookmarklet that only dumps inline globals to the console. I found window.Audio and some other built-in functions to make toString () return "[object Function]".
Is it possible to build functions (does this make sense?) Whose .toString () returns "[object Function]" without overriding .toString () In other words, is it possible that I am skipping nonlinear modules because I am skipping "[object functions] "?
This question is part of me trying to polish my bookmarks: https://gist.github.com/4635185
source to share
Since it window.Audio
has a different method toString
than Object
( Audio.toString === Object.toString //false
)
, any other method function
or Object
toString
can be overwritten and can return[object Function]
Now people don't run and overwrite their toString
method functions . So you can do a great job with this approach. But whether it might be the reason for debugging or something else, someone might work out to do it.
Update:
A trivial ECMAScript 5 solution ( Compatibility ) is also possible
(function () {
var keys = Object.keys(window),
r = {};
for (var i = 0, j; j = keys[i]; i++) {
r[j] = window[j];
}
console.dir(r);
})();
Edit:
As you gratefully noted, the last answer doesn't work correctly (I don't even know what I was thinking)
Anyway, another approach could be
- Create a new blank iframe (near: blank)
- Get all properties
window
Properties included from the start - Insert all the others into the array
Example
function getNonNatives(window) {
var iframe = document.createElement("iframe");
iframe.src = "about:blank";
document.body.appendChild(iframe);
var natives = iframe.contentWindow,nonnatives = {};
for(var prop in window)
if(!natives[prop] && window[prop] !== null)
nonnatives[prop] = window[prop];
document.body.removeChild(iframe);
return nonnatives;
};
console.log(getNonNatives(window));
This way, you don't have to worry if someone overrides the Object / Functions method toString
.
I will not include the old code in this edit for readability sake
A comment
To answer the question in the comment, which !!~
does:
~
is a bitwise NOT operator that inverts the bits of its operand.
So, given this logic, it can be used as a shorthand with indexOf
to check if nothing was found ,
since it returns -1
if nothing is found yet a positive number.
~-1 == 0
and for example ~4 == -5
Then !!
used to convert it to boolean
!!0 == false
and !!-5 == true
This is mainly shorthand for
if( myString.indexOf("myValue") > -1)
source to share