Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ?

39 views
Skip to first unread message

Une Bévue

unread,
Jul 19, 2016, 11:55:51 PM7/19/16
to
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 () {
...
};

and another version :

function resolve() {
...
};

Une Bévue

unread,
Jul 20, 2016, 2:20:53 AM7/20/16
to
Le 20/07/2016 à 06:09, Stefan Ram a écrit :
> When I
>
> let a = function f( x ){ return x ? x + f( x - 1 ): 0; };
> console.log( a( 2 ));
> let b = a;
> a = null;
> console.log( b( 2 ));
>
> , I get
>
> 3
> 3
>
> , but when I
>
> let a = function( x ){ return x ? x + a( x - 1 ): 0; };
> console.log( a( 2 ));
> let b = a;
> a = null;
> console.log( b( 2 ));
>
> , I only get one
>
> 3
>
> ; the call »a( x - 1 )« will not be resolved anymore as intended
> (i.e., recursively), after »a« has been »moved« to »b«.

fine, thanks !

Michael Haufe (TNO)

unread,
Jul 20, 2016, 2:26:02 AM7/20/16
to
function resolve(){...}

"resolve" is now in scope for the current block

var resolve = function resolve(){...}

This form is redundant. For a slight variation:

var resolve = function foo(){...}

"resolve" is now in scope for the current block.

"foo" is in scope for the RHS function.

both names refer to the same function.

More information on this behavior:

<https://kangax.github.io/nfe/>

Thomas 'PointedEars' Lahn

unread,
Jul 20, 2016, 6:15:33 AM7/20/16
to
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.

Thomas 'PointedEars' Lahn

unread,
Jul 20, 2016, 6:16:01 AM7/20/16
to
Michael Haufe (TNO) wrote:

> var resolve = function resolve(){...}
>
> This form is redundant.

No, it is not.

Michael Haufe (TNO)

unread,
Jul 20, 2016, 8:16:08 AM7/20/16
to
On Wednesday, July 20, 2016 at 5:16:01 AM UTC-5, Thomas 'PointedEars' Lahn wrote:
> Michael Haufe (TNO) wrote:
>
> > var resolve = function resolve(){...}
> >
> > This form is redundant.
>
> No, it is not.

Correct in semantic details, but by "redundant" in this case I was using the synonym: "useless". I would also use the same word "redundant" if the code happened to be the following:

var foo = function bar(){ /* explicit example with no reference to bar */ }

Thomas 'PointedEars' Lahn

unread,
Jul 20, 2016, 9:08:59 AM7/20/16
to
Michael Haufe (TNO) wrote:

> […] Thomas 'PointedEars' Lahn wrote:
>> Michael Haufe (TNO) wrote:
>> > var resolve = function resolve(){...}
>> >
>> > This form is redundant.
>> No, it is not.
>
> Correct in semantic details, but by "redundant" in this case I was using
> the synonym: "useless".

AISB, it is useless if and only if the function is not referred to from
within its body.

> I would also use the same word "redundant" if the code happened to be the
> following:
>
> var foo = function bar(){ /* explicit example with no reference to bar */
> }

It is still not useless if the function modifies the variable value but a
reference to itself has to be kept (self-modifying code).

Michael Haufe (TNO)

unread,
Jul 20, 2016, 12:24:31 PM7/20/16
to
On Wednesday, July 20, 2016 at 8:08:59 AM UTC-5, Thomas 'PointedEars' Lahn wrote:
> Michael Haufe (TNO) wrote:
>
> > […] Thomas 'PointedEars' Lahn wrote:
> >> Michael Haufe (TNO) wrote:
> >> > var resolve = function resolve(){...}
> >> >
> >> > This form is redundant.
> >> No, it is not.
> >
> > Correct in semantic details, but by "redundant" in this case I was using
> > the synonym: "useless".
>
> AISB, it is useless if and only if the function is not referred to from
> within its body.

I overlooked your comment saying that.

> > I would also use the same word "redundant" if the code happened to be the
> > following:
> >
> > var foo = function bar(){ /* explicit example with no reference to bar */
> > }
>
> It is still not useless if the function modifies the variable value but a
> reference to itself has to be kept (self-modifying code).

Correct

Une Bévue

unread,
Jul 23, 2016, 12:52:54 PM7/23/16
to
Le 20/07/2016 à 15:08, Thomas 'PointedEars' Lahn a écrit :
> AISB, it is useless if and only if the function is not referred to from
> within its body.
It is the case, in fact.
Thanks a lot for this cristal clear answer.

Stanimir Stamenkov

unread,
Jul 28, 2016, 1:31:31 AM7/28/16
to

Une Bévue

unread,
Aug 1, 2016, 12:44:53 AM8/1/16
to
0 new messages