• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaScript functions and their parentheses

 
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am completely new to JavaScript, and am currently trying to wrap my head around the calling of functions without their trailing parentheses, like so:

(note: had to replace the "onload" so that the forum software didn't complain)



This is after having defined the function somewhere else in this manner:



Then there is the other variation on this:



How do you know when to tack on those parentheses? When do you leave them off? When there aren't any parens, how do you know you are dealing with a function (besides just remembering the fact)?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Katrina Owen:
I am completely new to JavaScript, and am currently trying to wrap my head around the calling of functions without their trailing parentheses, like so:

?



That does not call the function, it merely assigns the function to the onload property of the window.

Functions in JavaScript are first-class objects that can be referenced just like any other object. They can be passed as parameters or assigned to properties as you are doing here.

Then there is the other variation on this:




I think you mean:



This creates an anonymous function using a function literal and assigns it to the onload property.

How do you know when to tack on those parentheses?



When you want to invoke the function. Think of the trailing parens as the "invocation operator".

how do you know you are dealing with a function (besides just remembering the fact)



Again functions are just data types like any other object. Any reference in JavaScript can be to any type of JavaScript object: a date, a string, a number, a DOM element, and even a function.
[ July 22, 2007: Message edited by: Bear Bibeault ]
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that helps make sense of it.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a very good website on JavaScript tutotial. Please go through it also.

http://www.webreference.com/programming/javascript/diaries/9/
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link, Suman. I've been looking at (well, reading, actually) various tutorials, but a lot of them seem to miss a lot of important points.

I've found a couple of books I'd like to look into, but until I get ahold of them, I'm going to continue looking at resources I find on the web.
reply
    Bookmark Topic Watch Topic
  • New Topic