Function fName() vs. fName = function() [FMX]

Ok, so I am messing around with some code and noticed that there is a difference between these two function declerations.

function fName(){//}
fName = function(){//}

I’ve always used function fName() before, but I am starting to learn some OOP, and figured I could change to fName = function() to indicate that it is a constructor. I thought it was a pretty good idea, until my code started behaving off.

An example of this is:


function Animal() { // Animal Constructor
}
Cat.prototype = new Animal(); 
function Cat() { // Cat Constructor
} 

If I type the “cat constructor” as Cat = function() it doesn’t work unless its before “cat.prototype”, now if I leave it as is, it will work if its called before or after the cat.prototype.

Why is that, should I NEVER use** fName = function()** to declare a function, or is my example just off? I guess I’m just looking for an explanation of this.

nothing is accessible before its defined except function Name() declared functions. Its not a problem with Name = function definitions, its just a feature of function Name definitions.

There are a few differences and each have their place. One difference you just pointed out. Another is that you can’t define functions through targets or object methods using the function Name style. Prototype methods, for example, have to use Class.prototype.name = function(){ … } Contrary to that, AS 2 methods have to use function name(){ … }

Their just different. Niether is necessarily right or wrong.