JQuery.isWindow method?

I tried to understand what I could, from jQuery animated functions, but ended up encountering all kinds of internal functions that I did not understand, and ultimately landed on isWindow. The code for isWindow checks to see if the object has the setInterval property and returns false otherwise.

Of course, any object can have the setInterval property without being a window, and although it should almost be a deliberate attempt to sabotage jQuery functionality in order to have an object with such an exact property name, I can present some reasonable cases when this may be unintentional.

Is there a better way to check if an object is a window object? Could they use something line by line

 obj.setInterval && obj.setInterval.toString() == 'function setIternval(){ [native code] } 

I know that returning the toString internal function will not be standard on all browsers, but jQuery authors seem to have a great understanding of these differences between browsers. I also know that this is also not a stupid method, since someone can easily override the toString method to return the same string, but this still interferes with the problem that the object is wrong for the window.

I would not ask if I thought that isWindow used only for internal objects using jQuery, but it was part of isPlainObject , which is used in .extend , which can be used for external objects.

+6
source share
1 answer

What about:

 function isWindow(obj) { var toString = Object.prototype.toString.call(obj); return toString == '[object global]' || toString == '[object Window]' || toString == '[object DOMWindow]'; } 

Seems to work in Chrome, Firefox, Opera, IE and Safari (newest versions)

+3
source

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


All Articles