0

I am curious why the browser handles .innerWidth and .innerWidth() differently. If I call window.innerWidth() the console outputs an error saying 'window.innerWidth is not a function.' When I call window.innerWidth it outputs the pixel width like I expect. But when I call something like $('random_div').innerWidth it outputs many lines of code, not what I expect. Though, $('random_div').innerWidth() outputs what I expect.

There is no explanation of the differences in the jQuery documentation that I have found. http://api.jquery.com/innerwidth/

https://jsfiddle.net/cyclingpaper/9fmwjLwq/

Thanks for your time.

  • 4
    A function without `()` is a *function reference*. When the parenthesis are there, it's calling the function. – Sterling Archer Dec 10 '15 at 14:50
  • 2
    If you're using `window.innerWidth` you're just accessing the `innerWidth` property of the window object - it has nothing to do with jQuery. – BrynJ Dec 10 '15 at 14:50
  • So window.innerWidth is a javascript function? While the innerWidth() is jQuery? – Cycling Paper Dec 10 '15 at 14:51
  • `window.innerWidth` isn't a function but a property – A. Wolff Dec 10 '15 at 14:52
  • 2
    That's not really a dupe imo, here the ambiguity is that jQuery used a window property name to use it as prototype function, it's jQuery after all... What if someone type `innerWidth` in google? – A. Wolff Dec 10 '15 at 15:05

1 Answers1

2

On the one hand there's the property innerWidth of the window object:

window.innerWidth;

On the other hand jQuery has a function .innerWidth() which can be called on a jQuery object.

And last but not least, you can get a reference to a function by calling it without the braces (). That's why calling .innerWidth on a jQuery object will return its function body.

insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
  • I didn't downvoted but there is no function `$.innerWidth`, i guess you mean `$.fn.innerWidth` the jq prototype one – A. Wolff Dec 10 '15 at 14:56
  • @A.Wolff Is there still content wise something wrong with this answer? Or is my wording not technical enough in this case? – insertusernamehere Dec 10 '15 at 15:00
  • 1
    Don't know but looks like someone downvoted all answers here... The question should be downvoted, not really the answers. And you get my vote because imho it is releavnt answer – A. Wolff Dec 10 '15 at 15:02
  • 1
    It wasn't me that down voted them.. most of them were helpful to my understanding. Thanks for your response though :D – Cycling Paper Dec 10 '15 at 15:06