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


Groups > comp.lang.javascript > #30931

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

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ?
Date 2016-07-20 12:15 +0200
Organization PointedEars Software (PES)
Message-ID <8980445.nUPlyArG6x@PointedEars.de> (permalink)
References <nmmso1$uof$1@shakotay.alphanet.ch>

Show all headers | View raw


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.

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web