A clean way of checking if an object is an instance of window.constructor

The name says a lot about everything. I need to check if the object is an instance of the DOM: Window interface. windowthe test will pass window.frames[xyz]if an iframe exists.

The most intuitive way is to simply check through object instanceof window.constructor. It is sad that there are browsers (e.g. IE6) whose window.constructorequal undefined.

What would you suggest? There are always hacker, ugly, and toStringaddictive ways, such as /\[object.*window.*\]/i.test(object), but I would rather go for a simple, clean solution, if possible.

+5
source share
2

window window, window. , - , window.constructor:

function isWindow(obj) {
    if (typeof(window.constructor) === 'undefined') {
        return obj instanceof window.constructor;
    } else {
        return obj.window === obj;
    }
}

jsFiddle,

+4

, , DOM (, closed):

function isWindow(obj)
{
    return typeof obj.closed !== "undefined";
}

.

+2