function expression:
//someFunction(); //wouldn't work when uncommented
var someFunction = function(){ alert('yay'); };
The func expression in this case is anonymous but assigned to a var for reference. This is different from a labeled function statement in the following ways:
- it can't be hoisted (called before it's defined)
new someFunction().constructor.name === 'someFunction';//false
instances don't get the var name for constructor.name because a reference to the function is assigned to the var but the var, not the function, is tied to the var name
In a labeled function statement:
//someFunction(); //works when uncommented
function someFunction(){ alert('yay'); }
- hoisting works
new someFunction().constructor.name === 'someFunction'; //true
the name is tied directly to the function.
Generally speaking there's not really any great reason to do expression to var unless you want calls to fail if things get moved around or you're defining/assigning a method in one line. I actually find hoisting useful for organizing objects with internal func and method definitions at the bottom so I can get to the actual behavior of the object and do one-line public method definitions (by just assigning funcs to this.
with the same name) all in one spot for ease of reference. You should always try to use labeled statements for constructors, IMO, so you can identify an object's 'type' via its constructor.