How can I get the window in which the object was defined?

I would like to check if an object is an instance of a specific built-in class. The problem is that my verification code may not be in the window where the object is defined, so it x instanceof Stringwill return false, although it xis String. What do I need, is it something like x instanceof getWindowOf(x)['String']? But is it possible to define such a function as getWindowOf?

Or we could solve it differently if we could get all the windows of the JS application. top, and the recursive loop is top.framesapproaching through , but pop-ups will not appear to us.

So what is the solution?

Please note that I just use Stringas an example. I really want to check out classes like Elementor any other classes defined in the browser.

+3
source share
1 answer

In order to avoid the problems with several frames that you have with the operator instanceof, and how you want to check a specific built-in object, I would recommend that you use a method Object.prototype.toString, it returns a string containing an internal property [[Class]], for example, assuming that it stris a string and arris an Array object like from another frame:

Object.prototype.toString.call(str); // returns "[object String]"
Object.prototype.toString.call(arr); // returns "[object Array]"
// while
str instanceof String; // is false
arr instanceof Array; // is false
// and 
typeof arr; // "object"

Additional Information:

+1
source

Source: https://habr.com/ru/post/1733365/


All Articles