Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #8030
| From | "Richard Cornford" <Richard@litotes.demon.co.uk> |
|---|---|
| Newsgroups | comp.lang.javascript |
| References | <4eb4a8de$0$28686$a8266bb1@newsreader.readnews.com> |
| Subject | Re: defining functions |
| Date | 2011-11-05 04:35 +0000 |
| Message-ID | <ZeWdnYj57omGICnTnZ2dnUVZ8t2dnZ2d@giganews.com> (permalink) |
Denis McMahon wrote:
> While I'm waiting for the faq to load so I can see if it says there
> in a manner that I can find, what's the difference between the
> function definition:
>
> var x = function(y)
> {
> // do something
> return;
> }
That is not a function definition, in ECMAScript terminology (so best
not called one). It is a variable declaration with assignment, where the
value being assigned is the result of evaluating a function expression.
> and the function definition:
>
> function x(y)
> {
> // do something
> return;
> }
>
> and why might someone choose to use one form over the other when
> their function calls are of the form:
>
> x(y);
In terms of this style of function call there is no advantage of one
over the other.
The advantages of one over the other are that the function declaration
is less code to write and the actual creation of the function object
will occur unconditionally upon entering the execution context in which
the declaration occurs. Assigning a function expression to a variable is
necessary if the function is to be created conditionally.
My position is that it is clearer (e.i. the textual distinction server
to add information to the source code by highlighting a real
distinction) and more direct to use a proper function declaration in all
circumstance where the result needs to be referred to by name and that
is practical. It is a position that appears to be unfashionable at
present, but for no apparent good reason.
Richard.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
defining functions Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-05 03:09 +0000
Re: defining functions "Richard Cornford" <Richard@litotes.demon.co.uk> - 2011-11-05 04:35 +0000
Re: defining functions Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-05 14:46 +0100
Re: defining functions "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-05 19:19 -0200
Re: defining functions Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-07 13:26 +0100
Re: defining functions "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-13 21:39 -0200
Re: defining functions Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-14 19:49 +0100
Re: defining functions "J.R." <groups_jr-1@yahoo.com.br> - 2011-11-14 17:00 -0200
csiph-web