minor fixes
This commit is contained in:
parent
319f77d6c1
commit
d41fcbf18a
1 changed files with 31 additions and 0 deletions
|
@ -225,6 +225,37 @@ In the example above, `anotherFunction()` isn't called at all, if the `text` par
|
||||||
On the other hand, it's independently called every time when `text` is missing.
|
On the other hand, it's independently called every time when `text` is missing.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
````smart header="Default parameters in old JavaScript code"
|
||||||
|
Several years ago, JavaScript didn't support the syntax for default parameters.
|
||||||
|
|
||||||
|
So people used some other ways to specify default values, that you meet in old scripts.
|
||||||
|
|
||||||
|
For example, an explicit check for `undefined`:
|
||||||
|
|
||||||
|
```js
|
||||||
|
function showMessage(from, text) {
|
||||||
|
*!*
|
||||||
|
if (text === undefined) {
|
||||||
|
text = 'no text given';
|
||||||
|
}
|
||||||
|
*/!*
|
||||||
|
|
||||||
|
alert( from + ": " + text );
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
...Or using the `||` operator:
|
||||||
|
|
||||||
|
```js
|
||||||
|
function showMessage(from, text) {
|
||||||
|
// If the value of text is falsy, assign the default value
|
||||||
|
text = text || 'no text given';
|
||||||
|
...
|
||||||
|
}
|
||||||
|
```
|
||||||
|
````
|
||||||
|
|
||||||
|
|
||||||
### Alternative default parameters
|
### Alternative default parameters
|
||||||
|
|
||||||
Sometimes it makes sense to assign default values for parameters not in the function declaration, but at a later stage.
|
Sometimes it makes sense to assign default values for parameters not in the function declaration, but at a later stage.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue