Path: csiph.com!news.swapon.de!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: difference between "var resolve = function resolve() {...}" and "var resolve = function () {...}" ? Date: Wed, 20 Jul 2016 12:15:24 +0200 Organization: PointedEars Software (PES) Lines: 83 Message-ID: <8980445.nUPlyArG6x@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1469009727 23927 eJwFwYEBwDAEBMCVorxnnCD2H6F3UBdvmsMNizUiZyrHis1VBtYl9UP4HZWTk5fJmvNC+/0WCxD2 (20 Jul 2016 10:15:27 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Wed, 20 Jul 2016 10:15:27 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwVy8EBwCAIA8CVRALqOCGW/Udo+78LT0stZCSio6034qihfUGYPDlYjHXFmY8+mM/o6zUN0iH53xq7fRZeaT4WDw== Cancel-Lock: sha1:9HIHu9I3JWZvjtezEgfUatPPb4k= X-NNTP-Posting-Host: eJwFwYkBwEAEBMCWnGdRDkL/JWTGBA/jCoPa2RWM6t5HEhubCp5MINIn4ph41MqvZ7slvX8TvBEc Xref: csiph.com comp.lang.javascript:30931 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 > ) > > 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: | SVN: Twitter: @PointedEars2 | ES Matrix: Please do not cc me. / Bitte keine Kopien per E-Mail.