jQuery的.isWindow方法? [英] jQuery's .isWindow method?
问题描述
我试图了解我可以从jQuery的动画函数中获得什么,但是最终遇到了我不了解的各种内部函数,最终落入了isWindow. isWindow的代码检查对象是否具有属性setInterval
,否则返回false.
I was trying to understand what I could from jQuery's animation functions, but ended up running into all sorts of internal functions I didn't understand, and ultimately landed on isWindow. The code for isWindow checks to see if an object has the property setInterval
, and returns false otherwise.
当然,任何对象都可以具有属性setInterval
而不是窗口,尽管它几乎是故意企图破坏jQuery的功能以具有具有确切属性名称的对象,但我可以想象一些合理的选择可能是无意的情况.
Of course, any object could have the property setInterval
without being the window, and although it would almost have to be a deliberate attempt to sabotage jQuery's functionality to have an object with that exact property name, I can imagine some reasonable cases where it could be unintentional.
是否有更好的方法来检查对象是否为窗口对象?他们不能使用类似
Is there not a better way to check if an object is a window object? Couldn't they use something along the lines of
obj.setInterval && obj.setInterval.toString() == 'function setIternval(){ [native code] }
我知道内部函数的toString
返回在所有浏览器中都不是标准的,但是jQuery的作者似乎对这些跨浏览器的差异有很好的理解.我也意识到这也不是一个简单的方法,因为有人可以轻松地覆盖toString
方法以返回相同的字符串,但是这仍然可以避免将对象误认为窗口的问题.
I know the return of toString
of an internal function isn't going to be standard across all browsers, but the writers of jQuery seem to have a great understanding of these cross-browser differences. I'm also aware that this isn't a fool-proof method either, as someone could easily override the toString
method to return that same string, but this would still prevent the problem of having an object mistaken for a window.
我不问我是否认为isWindow
仅由jQuery用于内部对象,但是它是isPlainObject
的一部分,而isPlainObject
在.extend
中使用了,可以在外部对象上使用.
I wouldn't ask if I thought that isWindow
was only used on internal objects by jQuery, but it was part of isPlainObject
, which is used in .extend
, which can be used on external objects.
推荐答案
有关:
function isWindow(obj) {
var toString = Object.prototype.toString.call(obj);
return toString == '[object global]' || toString == '[object Window]' || toString == '[object DOMWindow]';
}
似乎可以在Chrome,Firefox,Opera,IE和Safari(最新版本)中工作
Seems to work in Chrome, Firefox, Opera, IE and Safari (Newest versions)
这篇关于jQuery的.isWindow方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!