What is the difference between $(window) and window in jquery/javascript
I’m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%). In this category (stackoverflow) of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread here.
My quesiton was:
What is the difference between javascript window and jquery $(window)?
I tried in the Chrome console and I get this:
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…)
The answer, by user Elias Van Ootegem was:
When you write
$(window)
, you should know that that piece of code is going to run on the JS engine. Ever why jQ objects all have parentheses around them? Simple because$
is a function object. Basically you’re calling the$
function, and passing the native global, orwindow
object to it as an argument.If you browse through the jQ source code, you’ll see that it’ll pass that object on to many internal functions and in the end, it’ll return a jQ wrapper object.
So yes, your assumptions are pretty much correct.
Leave a Comment