www.webdeveloper.com
Results 1 to 3 of 3

Thread: Difference between ways for declaring a function

  1. #1
    Join Date
    Jul 2010
    Posts
    1

    Difference between ways for declaring a function

    Hi all,
    This is probably an easy question, but I was not sure of how to google an answer for it. Is there any real difference between these two function declarations?

    function DoSomething(parm) {
    //do something here
    }

    DoSomething = function(parm) {
    //do something here
    }

    The same goes when defining a custom object. For example:

    function MyObject() {
    //initialization code
    }

    MyObject = function() {
    //initialization code
    }

    Thanks.

  2. #2
    Join Date
    Jul 2010
    Posts
    135
    I think there is no difference, depends on your preference.

  3. #3
    Join Date
    Aug 2007
    Posts
    3,767
    As far as I know, whether you assign an anonymous function to a variable or declare it directly is pretty much irrelevant. The only slight difference I can think of is in a closure.
    Code:
    (function() {
    function func1 () {
    
    }
    var func2 = function () {
    
    };
    })();
    func1; // undefined
    func2; //defined
    Obviously if you "var func2 = ..." that difference vanishes.

    Both can be used for different purposes however. I normally use "function whatever ()" syntax unless I specifically want to assign it to an existing variable, or to an array or something that would be impossible with function whatever().

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles