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