为了学习 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.
最佳答案
他们这样做是为了,如果其他脚本错误地(或故意)创建了名为 document
、navigator
或 location
的变量>,它们不会影响 jQuery 对这些变量的使用。
关于javascript - jQuery 库代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11238582/