18

Are they native properties of window if so why is it called jQuery, surely jquery came after 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 = window.jQuery,

_$ = window.$,
124697
  • 22,097
  • 68
  • 188
  • 315
  • I would check this article out to learn more about fundamental jQuery. http://jqfundamentals.com/chapter/jquery-basics. Hope that helps! – David Ziemann Jun 04 '13 at 16:12
  • http://stackoverflow.com/questions/10896749/what-does-function-function-window-jquery-do –  Jun 04 '13 at 16:13

3 Answers3

16

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:

// 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.

Basically, jQuery (when you include it) creates functions at window.$ and window.jquery. Then it sets $ equal to both of those to $ for convenience.

David Ziemann
  • 960
  • 1
  • 8
  • 22
  • So to declare a global variable in javascript you must do something like `window.myVariableName` ? – 124697 Jun 04 '13 at 16:19
  • 2
    A global variable can be declared as myVariable. It gets bound to window.myVariable, and can be accessed using both myVariable and window.myVariable – David Ziemann Jun 04 '13 at 16:24
4

jQuery is a javascript library

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/

Once you include the script on the page it will create the objects jQuery and $ on the global context (window). It is not native.

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

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
2

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);

Any global is going to be a property of it.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335