admin管理员组

文章数量:1193808

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

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

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

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

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jun 5, 2013 at 11:52 NikolaNikola 15k21 gold badges106 silver badges170 bronze badges 5
  • 10 Your conclusion is pretty much correct. No further explanation needed, in my opinion. – nnnnnn Commented Jun 5, 2013 at 11:55
  • As a matter of fact you are right. – Igarioshka Commented Jun 5, 2013 at 11:55
  • 1 Something similar too jQuery $(this) vs this! – palaѕн Commented Jun 5, 2013 at 11:58
  • 6 $(window)[0] === window – eric_zyh Commented Jun 5, 2013 at 11:59
  • window.$(window) === $(window) – Néstor Waldyd Commented Jul 15, 2022 at 5:06
Add a comment  | 

4 Answers 4

Reset to default 15

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.

You are ture

window, which is a jQuery wrapper containing the global window object. The intent here was to create a locally-scoped window variable that would give me immediate access to jQuery methods like width(), height(), scrollLeft(), and scrollTop().

window is a global object and have no relation to any 3rd party library. however $(window) returns a jQuery object. You are right that its nothing but a wrapper but it comes with all possible jQuery goodies. We can use it just like normal jQuery object we can access its childs, can associate data wtih it etc etc.

The window object represents the window itself. You can find more explanation here. From what you describe above, it seems that you are looking to access the document properties instead of window properties. You can access the properties length, height, etc. as follows:

  • document.height (pure javascript) or $(document).height() (jQuery)
  • document.width (pure javascript) or $(document).width() (jQuery)

For more document properties, see here.

本文标签: What is the difference between (window) and window in jqueryjavascriptStack Overflow