3

What is the difference between this:

var doSomething=function()
{
    //do blah blah blah...  
}

And this:

function doSomething()
{
    //do blah blah blah...  
}

Another question: In PHP, we create a function by doing this:

function doSomething(a,b)
{
    //do something to variable a and b...
}

In JavaScript, we may have an object before the function:

object.doSomething(a);

My second question is, how would you create a function which requires an object in JavaScript?

Mateen Ulhaq
  • 18,406
  • 13
  • 75
  • 112
theHack
  • 1,690
  • 9
  • 22
  • 32
  • difference is that second function is defined at parse-time for a script block, whereas the first function is defined at run-time – Kris Ivanov Feb 16 '11 at 02:11
  • possible duplicate of [var functionName = function() {} vs function functionName() {}](http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) – Malachi Jul 13 '15 at 19:53

4 Answers4

6

The number one Google result for "function statement vs expression javascript" is another Stack Overflow question:

What is the difference between a function expression vs declaration in JavaScript?

It references the following article, which is the definitive reference on the subject:

http://kangax.github.com/nfe/

Community
  • 1
  • 1
Wayne
  • 56,476
  • 13
  • 125
  • 118
  • 1
    For questions related to the javascript language itself, I found the book "JavaScript: The Definitive Guide", By David Flanagan - really helpful. However, for this particular question, I believe the links provided are an even better source =) – rsalmeidafl Feb 16 '11 at 02:21
1

The difference between var fun = function() {} and function fun() {} is that in the first case it is stored in the variable fun. To call the function, you have to call fun(). Having it in a variable lets you pass the function around.

You can create objects by using functions

function MyClass() {
    this.fun = function() { alert('Hello world'); }
}

var obj = new MyClass();
obj.fun();

or JSON

var obj = {
   fun: function() { alert('Hello world'); }
};

obj.fun();

You can further extend the objects or their prototypes with new functions.

Edit. Sorry for the wrong answer: one shouldn't try to do these kinds of things at 4 am.

Aleksi Yrttiaho
  • 7,436
  • 24
  • 35
  • 2
    @Aleksi: Even for the second function you need to call fun(); at some point. – Praneeth Feb 16 '11 at 02:11
  • 1
    In neither case is the function executed immediately. To invoke the function immediately it would have to be var fun = function(){}(); or function fun() {}() – Jeff Feb 16 '11 at 02:11
  • 1
    The body of the function is not executed immediately in either case. – Wayne Feb 16 '11 at 02:11
  • You are absolute right, my mistake. Both are definitions, not calls. The first one just lets you use a functions as a variable and pass it around. Upvotes allaround for rapid corrections! – Aleksi Yrttiaho Feb 16 '11 at 02:15
  • Your first example is completely wrong, you're setting 'fun' on the window object and then setting obj to `undefined`. – david Feb 16 '11 at 03:15
  • @david Not completely - just missing the new keyword. I stand by my assertion that these answers shouldn't be written at 4 am. – Aleksi Yrttiaho Feb 16 '11 at 03:41
0

One question at a time.

To answer your first question, there is not a huge difference.

function doSomething() {} 

is technically equivalent to:

var doSomething;
doSomething = function() {};

technically what happens in this case is the variable declaration gets hoisted to the top of your script.

Jeff
  • 3,989
  • 21
  • 32
  • They're not quite equivalent, the first one hoists the definition as well as the declaration, the second only hoists the declaration. – david Feb 16 '11 at 03:12
0

For the second part of the question, we just do something like

object.doSomething = function(a) { ... }

which is one reason the function literal is so useful.

Ken Wayne VanderLinde
  • 17,085
  • 2
  • 42
  • 66