Forum Moderators: open

Message Too Old, No Replies

functions in js

         

AffiliateDreamer

4:23 pm on Jun 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

What is the different between:

var blah = function (a..n) { // code };

and

function blah(a..n)
{
// code
};

Are there many ways of defnining a function? What is a function literal and a function statement? (I see people defining it in so many ways I'm a little confused!).

poppyrich

5:20 pm on Jun 23, 2008 (gmt 0)

10+ Year Member



No difference between the two ways of creating a function you've shown. Six of one, half dozen of the other, as the phrase goes.

In both cases you've created a function named blah.

Which style to use is:

1)A matter of personal preference (You say tomayto, I say tomahto.)

2) The context within which the function is being declared could, possibly, play a part. For example, if you're declaring the function inside another function, it might feel more right to declare it as part of a larger sequence of variable declarations, as is the case with the function bar in the following:

function foo(){
var d=document,dE=d.documentElement,bar=function(e){return [e.clientWidth,e.clientHeight]};
alert('Viewport width is: '+bar(dE)[0]);
};

Bottom line advice: Whatever is clearer to your brain, do it that way.
It's only syntax, no functional difference.