5

Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}
What is the difference between a function expression vs declaration in Javascript?

I am attempting to understand the "best practices" of javascript.

This code is from jqfundementals.com

// create a function that will greet a person,
// and assign the function to the `greet` variable
var greet = function( person, message ) {
  var greeting = 'Hello, ' + person + '!';
  log( greeting + ' ' + message );
};

greet( 'Jory', 'Welcome to JavaScript' );
greet( 'Rebecca', 'Thanks for joining us' );

Why should I assign the function to the greet variable?

My first impulse would be to write it like this:

function greet ( person, message ) {
  var greeting = 'Hello, ' + person + '!';
  log( greeting + ' ' + message );
};

What are the differences between these two implementations?

Community
  • 1
  • 1
Nathan Koop
  • 23,022
  • 23
  • 86
  • 121

2 Answers2

1

There aren't any differences between those snippets, except hoisting which allows you to call the former function in the lines before the definition. But this is just a simplistic example to get you warmed up. In reality, people don't assign these functions to variables but pass them directly to other functions. Or they otherwise use them in expression contexts. Or they dynamically decide which function to to store. Or anything else really.

-2

There is no real difference but the var form enables you to declare-before-use incase you have recursive functions.

Simple example:

var func1, func2;

func1 = function (count) {
    count = count - 2;
    if (count > 0) {
        func2(count);
    }
}

func2 = function (count) {
    func1(count + 1);
}

func1(10);

Although

function func1 (count) {
    count = count - 2;
    if (count > 0) {
        func2(count);
    }
}

function func2 (count) {
    func1(count + 1);
}

func1(10);

is perfectly acceptable too. The interpreter will replace it with the former because of variable hoisting.

Halcyon
  • 54,624
  • 10
  • 83
  • 122
  • 1
    This is not correct. For function *statements* (which are different from function *expressions*), declared as `function name ..`, `name` has the same scope and hoisting of a `var name` variable. –  Sep 10 '12 at 18:23
  • 1
    function declarations are subject to hoisting, and if a function is called inside another function, it is looked for at runtime, not parse time — so that reasoning is doubly wrong. – Quentin Sep 10 '12 at 18:23
  • 1
    Declare before use is not enforced in JavaScript, variable hoisting is applied instead. What is your argument? – Halcyon Sep 10 '12 at 18:25
  • 1
    The argument is about the language used in this post "var form allows you to declare-before-use" well, no, not really. It **doesn't** "allow" this any more than the other "perfectly acceptable" form. Adding to the misguidance is the idea that the *placement* of `var` has some effect .. –  Sep 10 '12 at 18:27
  • Fixed the language. Also, judging by the sample code he posted, I doubt an in depth answer is required. For all intents and purposes these forms are equivalent. – Halcyon Sep 10 '12 at 18:28