Wednesday, November 18, 2015

Great article about javascript function declarations vs function expressions

Function Declarations vs. Function Expressions

And there are other reasons to favour Function Expressions?
How did you guess?
a) Function Declarations feel like they were intended to mimic Java style method declarations but Java methods are very different animals. In JavaScript functions are living objects with values. Java methods are just metadata storage. Both the following snippets define functions but only the Function Expression suggests that we are creating an object.

//Function Declaration
function add(a,b) {return a + b};
//Function Expression
var add = function(a,b) {return a + b};

Compare to C# Anonymous functions:

Anonymous Functions (C# Programming Guide)