Une Bévue wrote:
> That's the first time I've seen such writing :
>
> var resolve = function resolve() {
> ...
> };
>
> (see 'z-awf-as-promise.js' in
> <
https://gist.github.com/tunnckoCore/fd4d86b5dd62e5b1c10d>)
>
> and I wonder the difference(s) with :
>
>
> var resolve = function () {
> ...
> };
The difference is that with the former form (named function expression)
identifier resolution for “resolve” can stop at the function level; the name
of the function is treated as if it were a local variable. The scope chain
is one step shorter then, which is slightly more runtime-efficient. Also,
the function is easier reusable as it does not depend on the name of the
variable declared in the calling execution context. In fact, the names of
the variable and of the function do not have to be the same:
var foo = function bar () {
if (recurseCondition) bar();
};
You only need a named function expression if you need to refer to the
function from within the function context.
In previous versions of Microsoft JScript, the first form was error-prone
because the name of the function was available outside the function context.
> and another version :
>
> function resolve() {
> ...
> };
Above there are function expressions on the right-hand side.
This form is a function declaration; The trailing semicolon is superfluous.
Declaration are processed before all other code in the same execution
context, meaning that you can call the function by name even if the call is
located before the declaration. Also, there is proprietary behavior to
support function declarations in blocks, e.g.
if (foo)
{
function bar () {}
}
But because it is proprietary, you should not rely on it and write
if (foo)
{
var bar = function () {};
}
or
if (foo)
{
var bar = function whatever () {};
}
if you want to create the function conditionally. The variable (here: bar)
is available before this line, but the function cannot be called at this
point because the reference to it (functions are objects, Function
instances) has not been assigned to the variable yet.
The From header field value of your posting is disregarding the Netiquette.
Usenet is public communication in the newsgroup *and* private via e-mail.
--
PointedEars
FAQ: <
http://PointedEars.de/faq> | SVN: <
http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <
http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.