Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #30922 > unrolled thread

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

Started byUne Bévue <unbewusst.sein@fai.invalid>
First post2016-07-20 05:55 +0200
Last post2016-08-01 06:44 +0200
Articles 11 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ? Une Bévue <unbewusst.sein@fai.invalid> - 2016-07-20 05:55 +0200
    Re: difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ? Une Bévue <unbewusst.sein@fai.invalid> - 2016-07-20 08:20 +0200
    Re: difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-07-19 23:25 -0700
      Re: difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-07-20 12:15 +0200
        Re: difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-07-20 05:15 -0700
          Re: difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-07-20 15:08 +0200
            Re: difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-07-20 09:23 -0700
            Re: difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ? Une Bévue <unbewusst.sein@fai.invalid> - 2016-07-23 18:52 +0200
    Re: difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-07-20 12:15 +0200
    Re: difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ? Stanimir Stamenkov <s7an10@netscape.net> - 2016-07-28 08:31 +0300
      Re: difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ? Une Bévue <unbewusst.sein@fai.invalid> - 2016-08-01 06:44 +0200

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

FromUne Bévue <unbewusst.sein@fai.invalid>
Date2016-07-20 05:55 +0200
Subjectdifference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ?
Message-ID<nmmso1$uof$1@shakotay.alphanet.ch>
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() {
   ...
};

[toc] | [next] | [standalone]


#30925

FromUne Bévue <unbewusst.sein@fai.invalid>
Date2016-07-20 08:20 +0200
Message-ID<nmn57v$fdj$1@shakotay.alphanet.ch>
In reply to#30922
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 !

[toc] | [prev] | [next] | [standalone]


#30927

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2016-07-19 23:25 -0700
Message-ID<e66dc666-80eb-4a05-8650-4681e2cb7be4@googlegroups.com>
In reply to#30922
On Tuesday, July 19, 2016 at 10:55:51 PM UTC-5, 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 () {
>    ...
> };
> 
> and another version :
> 
> function resolve() {
>    ...
> };


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/>

[toc] | [prev] | [next] | [standalone]


#30932

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-07-20 12:15 +0200
Message-ID<1961081.iZASKD2KPV@PointedEars.de>
In reply to#30927
Michael Haufe (TNO) wrote:

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

No, it is not.

-- 
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.

[toc] | [prev] | [next] | [standalone]


#30934

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2016-07-20 05:15 -0700
Message-ID<941fff3e-5d05-4648-ba43-51e9ceb3ca7d@googlegroups.com>
In reply to#30932
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 */ }

[toc] | [prev] | [next] | [standalone]


#30950

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-07-20 15:08 +0200
Message-ID<1703874.PYKUYFuaPT@PointedEars.de>
In reply to#30934
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).

-- 
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.

[toc] | [prev] | [next] | [standalone]


#30951

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2016-07-20 09:23 -0700
Message-ID<4c3c798b-41ef-4a8d-ba27-9c783c3654dd@googlegroups.com>
In reply to#30950
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

[toc] | [prev] | [next] | [standalone]


#30968

FromUne Bévue <unbewusst.sein@fai.invalid>
Date2016-07-23 18:52 +0200
Message-ID<nn07d0$tkg$1@shakotay.alphanet.ch>
In reply to#30950
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.

[toc] | [prev] | [next] | [standalone]


#30931

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2016-07-20 12:15 +0200
Message-ID<8980445.nUPlyArG6x@PointedEars.de>
In reply to#30922
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.

[toc] | [prev] | [next] | [standalone]


#30986

FromStanimir Stamenkov <s7an10@netscape.net>
Date2016-07-28 08:31 +0300
Message-ID<nnc5bd$6pu$1@dont-email.me>
In reply to#30922
Wed, 20 Jul 2016 05:55:44 +0200, Une Bévue:

> 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() {
>    ...
> };

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions#Function_constructor_vs._function_declaration_vs._function_expression

-- 
Stanimir

[toc] | [prev] | [next] | [standalone]


#31000

FromUne Bévue <unbewusst.sein@fai.invalid>
Date2016-08-01 06:44 +0200
Message-ID<nnmk3v$cjp$1@shakotay.alphanet.ch>
In reply to#30986
Le 28/07/2016 à 07:31, Stanimir Stamenkov a écrit :

>
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions#Function_constructor_vs._function_declaration_vs._function_expression
>
>
thanks for the URL.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web