SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Apr 2004
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    functions: someName = function () {

    I was trying to understand this type of functions and how do they work, but can't find a good tutorial or page that refers to functions like this:

    someName = function () {
    ....
    ....

    }

    Does anybody knows a good resource that explains it?

    I was also wondering how to call a function that is set like this.

    Thanks a lot!

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    That is called an anonymous function since the function itself does not have a name and so can't be called from anywhere else. Basically where someName is an event handler the assignment attaches the anonymous function so that it will be run whenever that event occurs. The equivalent without using an anonymous function requires two statements - one to define the function and a second to attach it to the event handler eg.

    function someFunction() {
    ....
    ....

    }
    someName = someFunction;

    If someName is a variable rather than an event handler then it is effectively the name of the function and you'd call the function using someName();
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •