Javascript in the DOM has a peculiar characteristic. There's a different Object
object that an object (by default) inherits per window.
In order to find what kind of object is being sent to a function when an object from a different window may be given, the methods I've read to know what was given are the use of duck-typing and analysis of the output of .toString()
. Given that instanceof
is useless.
Duck-typing is done by checking if certain properties from an object in a variable exist or exist and have a value of certain type to confirm that an object is actually what is expected.
The idea I want to explore is an alternative to Duck-typing (even if it is slower) to get the actual object's constructor for when the object is one of the javascript's default objects in the DOM (Object, Array, Element, Function, etc.)
to try getting the window that has a certain object and then to try working from there.
In other words, is there a way to get the window from where an object was created at all times (assuming the object is an instance of Object
in the window it was created)?
If so, then how would you do such check?
My current idea is to get all the windows in the parent document something like:
var windows = [window.top];
var iframes = document.getElementsByTagName('iframe');
for(var i = 0; i < iframes.length; i++){
windows.push(iframes[i].contentWindow);
}
And then
function isATypeOfB(A, B){
for(var i = 0; i < windows.length; i++){
if(windows[i][A] && windows[i][A].prototype.isPrototypeOf(B)){
return true;
}
}
return false;
}
I already have the impression it doesn't work due to the "contentWindow" limited access to same-domain policy. I also don't show here checks to update the windows
array when the number of iframes change or the url of an iframe changes.
The main goal here is to be able to tell the constructor of a certain object instance that exists in a variable has a certain object in its prototype chain. For example, to know if a variable contains an instance of an Array
.
What are your views here?