2

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?

2
  • 1
    To clarify, is the ultimate goal here to access members of an object created from another window (and perhaps to know that they belong to the parent window)? Also, what is the use case, unless it's just academic? Commented Feb 25, 2016 at 20:44
  • @JeffreySweeney I added the "The main goal here" paragraph. Please confirm if that answers your question. Also, it is not made with an academic objective although, due to limitations it may have, it may just end up being academic
    – brunoais
    Commented Feb 26, 2016 at 11:49

2 Answers 2

0

What about the name attribute: MDN

<iframe src="www.example.com" name="myIframe1"></iframe>
<iframe src="www.example.com" name="myIframe2"></iframe>
<iframe src="www.example.com" name="myIframe3"></iframe>

Then just register them with the name attribute, and Observe them with another object. This gives you unique access as your listening.

class Subject {
constructor() {
this.obseverList = {};
}
register(observer){ // push observer }
}

class Iframe1 {}
class Iframe2 {}
class Iframe3 {}

const sub = new Subject();
sub.register(Iframe1)...

And so on... a more comprehensive version of the Observer Pattern can be found here

0

One way you could do that (which doesn't require keeping track of iframes) is to check what the object's prototype constructor is named:

const typeName = value => Object.getPrototypeOf(value).constructor.name

if (typeName(myObj) === "Object") {
    ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.