minor fixes
This commit is contained in:
parent
689975093c
commit
f6ff773033
24 changed files with 67 additions and 76 deletions
|
@ -41,7 +41,7 @@ Please note that the last line does not run the function, because there are no p
|
|||
|
||||
In JavaScript, a function is a value, so we can deal with it as a value. The code above shows its string representation, which is the source code.
|
||||
|
||||
It is a special value of course, in the sense that we can call it like `sayHi()`.
|
||||
Surely, a function is a special values, in the sense that we can call it like `sayHi()`.
|
||||
|
||||
But it's still a value. So we can work with it like with other kinds of values.
|
||||
|
||||
|
@ -61,9 +61,7 @@ sayHi(); // Hello // this still works too (why wouldn't it)
|
|||
Here's what happens above in detail:
|
||||
|
||||
1. The Function Declaration `(1)` creates the function and puts it into the variable named `sayHi`.
|
||||
2. Line `(2)` copies it into the variable `func`.
|
||||
|
||||
Please note again: there are no parentheses after `sayHi`. If there were, then `func = sayHi()` would write *the result of the call* `sayHi()` into `func`, not *the function* `sayHi` itself.
|
||||
2. Line `(2)` copies it into the variable `func`. Please note again: there are no parentheses after `sayHi`. If there were, then `func = sayHi()` would write *the result of the call* `sayHi()` into `func`, not *the function* `sayHi` itself.
|
||||
3. Now the function can be called as both `sayHi()` and `func()`.
|
||||
|
||||
Note that we could also have used a Function Expression to declare `sayHi`, in the first line:
|
||||
|
@ -93,7 +91,7 @@ let sayHi = function() {
|
|||
|
||||
The answer is simple:
|
||||
- There's no need for `;` at the end of code blocks and syntax structures that use them like `if { ... }`, `for { }`, `function f { }` etc.
|
||||
- A Function Expression is used inside the statement: `let sayHi = ...;`, as a value. It's not a code block. The semicolon `;` is recommended at the end of statements, no matter what is the value. So the semicolon here is not related to the Function Expression itself in any way, it just terminates the statement.
|
||||
- A Function Expression is used inside the statement: `let sayHi = ...;`, as a value. It's not a code block, but rather an assignment. The semicolon `;` is recommended at the end of statements, no matter what is the value. So the semicolon here is not related to the Function Expression itself, it just terminates the statement.
|
||||
````
|
||||
|
||||
## Callback functions
|
||||
|
@ -210,7 +208,6 @@ That's due to internal algorithms. When JavaScript prepares to run the script, i
|
|||
|
||||
And after all Function Declarations are processed, the code is executed. So it has access to these functions.
|
||||
|
||||
|
||||
For example, this works:
|
||||
|
||||
```js run refresh untrusted
|
||||
|
@ -239,6 +236,8 @@ let sayHi = function(name) { // (*) no magic any more
|
|||
|
||||
Function Expressions are created when the execution reaches them. That would happen only in the line `(*)`. Too late.
|
||||
|
||||
Another special feature of Function Declarations is their block scope.
|
||||
|
||||
**In strict mode, when a Function Declaration is within a code block, it's visible everywhere inside that block. But not outside of it.**
|
||||
|
||||
For instance, let's imagine that we need to declare a function `welcome()` depending on the `age` variable that we get during runtime. And then we plan to use it some time later.
|
||||
|
@ -291,7 +290,7 @@ if (age < 18) {
|
|||
|
||||
} else {
|
||||
|
||||
function welcome() { // for age = 16, this "welcome" is never created
|
||||
function welcome() {
|
||||
alert("Greetings!");
|
||||
}
|
||||
}
|
||||
|
@ -308,7 +307,7 @@ What can we do to make `welcome` visible outside of `if`?
|
|||
|
||||
The correct approach would be to use a Function Expression and assign `welcome` to the variable that is declared outside of `if` and has the proper visibility.
|
||||
|
||||
Now it works as intended:
|
||||
This code works as intended:
|
||||
|
||||
```js run
|
||||
let age = prompt("What is your age?", 18);
|
||||
|
@ -395,7 +394,7 @@ alert( sum(1, 2) ); // 3
|
|||
|
||||
```
|
||||
|
||||
If we have only one argument, then parentheses can be omitted, making that even shorter:
|
||||
If we have only one argument, then parentheses around parameters can be omitted, making that even shorter:
|
||||
|
||||
```js run
|
||||
// same as
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue