window.jQuery 和 window.$ 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16922592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does window.jQuery and window.$ mean?
提问by code511788465541441
Are they native properties of window
if so why is it called jQuery, surely jquery came after javascript
它们是本机属性,window
如果是这样,为什么它被称为 jQuery,肯定 jquery 是在 javascript 之后出现的
Edit: I was looking through jquery.js and found these two lines which made me wonder about what they mean exactly. If wouldn't window.Jquery be null since JQuery is not a variable of window?
编辑:我正在浏览 jquery.js,发现这两行让我想知道它们到底是什么意思。如果因为 JQuery 不是窗口变量,window.Jquery 不会为空吗?
_jQuery = window.jQuery,
_$ = window.$,
采纳答案by David Ziemann
I will pull from an article I linked to in a comment above:
我将从上面评论中链接到的一篇文章中提取:
As discussed in the JavaScript Basics section, valid names in JavaScript can be pretty much anything, as long as they don't begin with a number and don't include a hyphen. So, the $ in the code above is just a shorter, more convenient name for the jQuery function; indeed, in the jQuery source code, you'll find this near the end:
正如 JavaScript 基础部分所讨论的,JavaScript 中的有效名称几乎可以是任何名称,只要它们不以数字开头且不包含连字符。因此,上面代码中的 $ 只是 jQuery 函数的一个更短、更方便的名称;实际上,在 jQuery 源代码中,您会在接近尾声时发现:
// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;
When you call the $() function and pass a selector to it, you create a new jQuery object. Of course, in JavaScript, functions are objects too, so that means that $ (and jQuery, of course) has properties and methods, too. For example, you can refer to the $.support property for information on what the current browser environment supports, and you use the $.ajax method to make an AJAX request.
当您调用 $() 函数并将选择器传递给它时,您就创建了一个新的 jQuery 对象。当然,在 JavaScript 中,函数也是对象,所以这意味着 $(当然还有 jQuery)也有属性和方法。例如,您可以参考 $.support 属性以获取有关当前浏览器环境支持的信息,并使用 $.ajax 方法发出 AJAX 请求。
Basically, jQuery (when you include it) creates functions at window.$ and window.jquery. Then it sets $ equal to both of those to $ for convenience.
基本上,jQuery(当你包含它时)在 window.$ 和 window.jquery 创建函数。然后为方便起见,它将 $ 等于这两个设置为 $。
回答by BrunoLM
jQuery
is a javascript library
jQuery
是一个 javascript 库
jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript. http://jquery.com/
jQuery 是一个快速、小巧且功能丰富的 JavaScript 库。它通过易于使用的 API 使 HTML 文档遍历和操作、事件处理、动画和 Ajax 等工作变得更加简单,该 API 可在多种浏览器上运行。jQuery 结合了多功能性和可扩展性,改变了数百万人编写 JavaScript 的方式。 http://jquery.com/
Once you include the script on the page it will create the objects jQuery
and $
on the global context (window). It is not native.
一旦你包括页面上的脚本,它会创建对象jQuery
,并$
在全球范围内(窗口)。它不是原生的。
These
这些
_jQuery = window.jQuery,
_$ = window.$,
Are internal mappings in case of overwrite. You can use the function .noConflict
to restore the previous value of window.$
preventing conflicts with prototype
and other libraries
在覆盖的情况下是内部映射。您可以使用该功能.noConflict
恢复以前window.$
防止与prototype
其他库发生冲突的值
回答by Quentin
window
is the default / global object. Any time you assign a value, and aren't explicit about the object to which it will be attached then it is going to be assigned to a property of the global object unless there is a local variable to assign it to first (see section 3.b. of PutValue);
window
是默认/全局对象。任何时候您分配一个值,并且不明确说明它将附加到的对象时,它将被分配给全局对象的属性,除非有一个局部变量首先分配给它(参见第 3 节.b. PutValue);
Any global is going to be a property of it.
任何全局都将成为它的一个属性。