Closed
Bug 1243496
Opened 8 years ago
Closed 4 years ago
The function declaration and function expression documentation is incorrect
Categories
(Developer Documentation Graveyard :: JavaScript, defect, P5)
Tracking
(Not tracked)
RESOLVED
WONTFIX
People
(Reporter: RomenoEx, Unassigned)
References
()
Details
:: Developer Documentation Request Request Type: Correction Gecko Version: unspecified Technical Contact: :: Details The documentation about function declarations, function expression does not correspond to actual browser behavior. The docs says that the following samples are function expressions if (x) { // function expression function f1() {} } // function declaration function a() { // function declaration function b() {} if (0) { // function expression function f2() {} } } If it is then f1 and f2 can be a) executed immediately using () b) can have no name Both of these assumptions are false hence f1 and f2 are function declarations Moreover doing if (true) { // function expression function f1() {} } in empty browser script throws an error in Mozilla Firefox v43.0.4 saying SyntaxError: function statement requires a name It says just clear that it is function statement. As specified in the docs article function statement and function declaration is just the same thing. I think there is something wrong here.
Correction: Moreover doing if (true) { // function expression function () {} () // no name and immediately invoke it } in empty browser script throws an error in Mozilla Firefox v43.0.4 saying SyntaxError: function statement requires a name
What is a Function Declaration? A Function Declaration defines a named function variable without requiring variable assignment. Function Declarations occur as standalone constructs and cannot be nested within non-function blocks. It’s helpful to think of them as siblings of Variable Declarations. Just as Variable Declarations must start with “var”, Function Declarations must begin with “function”. e.g. function bar() { return 3; } function Identifier ( FormalParameterListopt ) { FunctionBody } The function name is visible within it’s scope and the scope of it’s parent (which is good because otherwise it would be unreachable) function bar() { return 3; } bar() //3 bar //function What is a Function Expression? A Function Expression defines a function as a part of a larger expression syntax (typically a variable assignment ). Functions defined via Functions Expressions can be named or anonymous. Function Expressions must not start with “function” (hence the parentheses around the self invoking example below) e.g. //anonymous function expression var a = function() { return 3; } //named function expression var a = function bar() { return 3; } //self invoking function expression (function sayHello() { alert("hello!"); })(); ECMA 5 (13.0) defines the syntax as function Identifieropt ( FormalParameterListopt ) { FunctionBody } (though this feels incomplete since it omits the requirement that the containing syntax be an expression and not start with “function”) The function name (if any) is not visible outside of it’s scope (contrast with Function Declarations). So what’s a Function Statement? Its sometimes just a pseudonym for a Function Declaration. However as kangax pointed out, in mozilla a Function Statement is an extension of Function Declaration allowing the Function Declaration syntax to be used anywhere a statement is allowed. It’s as yet non standard so not recommended for production development
Comment 3•4 years ago
|
||
MDN Web Docs' bug reporting has now moved to GitHub. From now on, please file content bugs at https://github.com/mdn/sprints/issues/ and platform bugs at https://github.com/mdn/kuma/issues/.
Status: UNCONFIRMED → RESOLVED
Closed: 4 years ago
Resolution: --- → WONTFIX
You need to log in
before you can comment on or make changes to this bug.
Description
•