Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed20.multikabel.net!amsnews11.chello.com!xlned.com!feeder1.xlned.com!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <2999732.SPkdTlGXAF@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Mon, 07 Nov 2011 13:26:44 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit Subject: Re: defining functions Newsgroups: comp.lang.javascript References: <4eb4a8de$0$28686$a8266bb1@newsreader.readnews.com> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 113 NNTP-Posting-Date: 07 Nov 2011 13:26:44 CET NNTP-Posting-Host: 8102a86a.newsspool4.arcor-online.net X-Trace: DXC=<^BR90C3k7=^cW`WBF>WQ<4IUK X-Complaints-To: usenet-abuse@arcor.de Xref: x330-a1.tempe.blueboxinc.net comp.lang.javascript:8101 J.R. wrote: > On 05/11/2011 01:09, Denis McMahon wrote: >> While I'm waiting for the faq to load so I can see if it says there in a >> manner that I can find, what's the difference between the function >> definition: >> >> var x = function(y) >> { >> // do something >> return; >> } >> >> and the function definition: >> >> function x(y) >> { >> // do something >> return; >> } >> >> and why might someone choose to use one form over the other when their >> function calls are of the form: >> >> x(y); > > Hi, > According to the excellent JavaScript Patterns book, by Stoyan Stefanov, > Chapter 4 - Functions, section "Declarations Versus Expressions: Names > and Hoisting" (p. 59): > > - Function declarations can only appear in 'program code', meaning > inside of the bodies of other functions or in the global space. Their > definitions cannot be assigned to variables or properties, or appear in > function invocations as parameters; Depending on what you mean by "their definitions", the second sentence may be wrong. For the following is certainly possible: function x(y) { console.log(y); } var z = x; var o = { p: x }; (function(f) { return f; })(o.p)(42); You would be correct if you said that a function declaration cannot appear right-hand side of an assignment operator or within an argument list of a function call. That is so simply because the parser is in Expression context there, and the FunctionDeclaration syntax is considered to be a named FunctionExpression there. > - the availability of the read-only name property of the function > definition pattern. This property is not standard but available in many > environments. In function declarations and named function expressions, > the name property is defined. In anonymous function expressions, it > depends on the implementation; it could be undefined (IE) or defined > with an empty string (Firefox, WebKit) [...] In Microsoft JScript, which you mistakenly and misguidingly referred to as "IE", at least in version 5.6.6626, Function instances have no `name' property in any case. > *And finally, the most important difference IMO lies in the hoisting ^^^^^^^^ > behavior*: ^^^^^^^^ I beg your pardon? > [...] all variables, no matter where in the function body they are > declared, get hoisted to the top of the function behind the scenes. Utter nonsense. > The same applies for functions because they are just objects assigned to > variables. That is oversimplifying talk; nothing is "hoisted" here. What really happens is that all declarations in source code are specified to happen before control reaches the first statement of the execution context. Variable instantiation adds a property to the ES 1 to 3 Variable Object, with the identifier of the variable or function as name [1]. In ES 5.x, this is described as Declaration Binding Instantiation, i. e. bindings added to a VariableEnvironment's Environment Record, instead, but it follows essentially the same pattern [2]. > I'd strongly suggest that you purchase and read this book carefully. I strongly suggest that you stop believing blindly in what book authors (anyone, really) say and start thinking for yourself. /Sapere aude!/ HTH PointedEars ___________ [1] Standard ECMA-262, "ECMAScript Language Specification", Edition 3 Final, section 10.1.3, "Variable Instantiation". Ecma International, Geneva, March 2000. [2] Standard ECMA-262, "ECMAScript Language Specification", 5.1 Edition, section 10.5, "Declaration Binding Instantiation". Ecma International, Geneva, June 2011. -- realism: HTML 4.01 Strict evangelism: XHTML 1.0 Strict madness: XHTML 1.1 as application/xhtml+xml -- Bjoern Hoehrmann