mirror of
https://github.com/airbnb/javascript.git
synced 2026-01-14 03:18:03 -05:00
advice against side effect in default parameter
This commit is contained in:
17
README.md
17
README.md
@@ -550,6 +550,23 @@
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
- Don't put side effect into default parameter
|
||||
|
||||
> it introduces confusion and subtlety. Arguments in function call are evaluated at call site, but default parameters are not evaluated at define site.
|
||||
|
||||
```javascript
|
||||
var b = 1
|
||||
// bad
|
||||
function count(a = b++) {
|
||||
console.log(a)
|
||||
}
|
||||
count() // 1
|
||||
count() // 2
|
||||
count(3) // 3
|
||||
count() // 3
|
||||
```
|
||||
|
||||
|
||||
**[⬆ back to top](#table-of-contents)**
|
||||
|
||||
|
||||
Reference in New Issue
Block a user