0

为了学习 JS OOP,我正在查看 jQuery 的源代码以更好地了解它们是如何做事的。我的问题可能看起来很简单,但我无法理解 jQuery 在其库顶部定义的几个变量背后的原因。代码如下所示。

(function( window, undefined ) {

// Use the correct document accordingly with window argument (sandbox)
var document = window.document,
    navigator = window.navigator,
    location = window.location;

....rest of code

What I don't understand is why they created variables for the document, navigator, and location objects.  Does this resolve some type of browser bug?  I don't understand the benefit of doing this.
4

1 回答 1

3

他们这样做是为了,如果其他脚本错误地(或有意地)创建了名为document,navigator或的变量location,它们不会影响 jQuery 对这些变量的使用。

于 2012-06-28T05:35:57.930 回答