Wednesday, November 18, 2015

Great article about javascript function declarations vs function expressions

Function Declarations vs. Function Expressions
https://javascriptweblog.wordpress.com/2010/07/06/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)
https://msdn.microsoft.com/en-us/library/bb882516.aspx