Improve/fix usage of bracket

This commit is contained in:
caspringer 2018-12-14 08:08:10 -05:00 committed by GitHub
parent 23b5766b82
commit 58ba9f797b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -475,7 +475,7 @@ They look like this:
Here a Function Expression is created and immediately called. So the code executes right away and has its own private variables. Here a Function Expression is created and immediately called. So the code executes right away and has its own private variables.
The Function Expression is wrapped with brackets `(function {...})`, because when JavaScript meets `"function"` in the main code flow, it understands it as the start of a Function Declaration. But a Function Declaration must have a name, so there will be an error: The Function Expression is wrapped with parenthesis `(function {...})`, because when JavaScript meets `"function"` in the main code flow, it understands it as the start of a Function Declaration. But a Function Declaration must have a name, so there will be an error:
```js run ```js run
// Error: Unexpected token ( // Error: Unexpected token (
@ -497,7 +497,7 @@ function go() {
}(); // <-- can't call Function Declaration immediately }(); // <-- can't call Function Declaration immediately
``` ```
So, round brackets are needed to show JavaScript that the function is created in the context of another expression, and hence it's a Function Expression. It needs no name and can be called immediately. So, parenthesis are needed to show JavaScript that the function is created in the context of another expression, and hence it's a Function Expression. It needs no name and can be called immediately.
There are other ways to tell JavaScript that we mean Function Expression: There are other ways to tell JavaScript that we mean Function Expression: