Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between $(window) and window in jquery/javascript

What is the difference between javascript window and jquery $(window)?

I tried in the Chrome console and I get this: enter image description here

So, I would conclude is "just" a window object wrapped in a jquery object in a way that then I can use jquery's functions on it (like height(), width(), etc...)

I did try googling, and stackoverlowing :) OFC, but to no luck.

like image 325
Nikola Avatar asked Jun 05 '13 11:06

Nikola


People also ask

What is window in jQuery?

Definition of jQuery window. jQuery window object represents an open window in the browser. The window object is automatically created by the browser. Suppose our documents contain multiple frames, then the browser creates one window object for the HTML document and creates additional window objects for each frame.

What does $() mean in JavaScript?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document.

What does $( this mean in jQuery?

$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . Calling jQuery a second time (which is a mistake) on the result of $(this) returns an new jQuery object based on the same selector as the first one.


1 Answers

When you write $(window), you should know that that piece of code is going to run on the JS engine. Have you ever wondered why jQuery objects all have parentheses around them? It is because $ is a function object. Basically you're calling the $ function, and passing the native global, or window object to it as an argument.

If you browse through the jQuery source code, you'll see that it'll pass that object on to many internal functions and in the end, it'll return a jQuery wrapper object.
So yes, your assumptions are pretty much correct.

like image 162
Elias Van Ootegem Avatar answered Nov 15 '22 14:11

Elias Van Ootegem