Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add examples for pure functions and destructuring defaults#704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
mohandand wants to merge1 commit intoryanmcdermott:master
base:master
Choose a base branch
Loading
frommohandand:pure-functions-and-destructuring-topics
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletionsREADME.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1033,6 +1033,40 @@ function newRequestModule(url) {
const req = newRequestModule;
inventoryTracker("apples", req, "www.inventory-awesome.io");
```
**[⬆ back to top](#table-of-contents)**

#### Pure Functions vs. Side Effects

- A **pure function** only uses its input values and doesn’t change anything outside itself.
- A **side effect** is any change to external state (mutating arguments, globals, etc.).

**Why?**
- Predictable: gives the same result every time for the same inputs.
- Easy to test: no hidden state to reset or mock.
- Safe to reuse and refactor without unintended consequences.

**Bad:**
```js
let total = 0;

function addToCart(cart, item) {
cart.push(item); // ← side effect: changing external cart
total += item.price; // ← side effect: changes external `total`
return cart.length;
}
```
**Good:**
```js
function addToCart(cart, item) {
// returns a new cart array without touching the original
return [...cart, item];
}

function calculateTotal(cart) {
// pure computation: only reads input, never writes out
return cart.reduce((sum, item) => sum + item.price, 0);
}
```

**[⬆ back to top](#table-of-contents)**

Expand DownExpand Up@@ -1136,6 +1170,35 @@ console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe
delete employee.name;
console.log(`Employee name: ${employee.getName()}`); // Employee name: John Doe
```
**[⬆ back to top](#table-of-contents)**

#### Destructuring Defaults & Nested Destructuring

It lets you set default values for object properties right in the function signature and prevents errors if a nested object isn’t provided.

**Why?**
- Eliminates repetitive `||` defaulting.
- You can see what values the function uses and their fallback values right in its definition.
- Prevents runtime errors when nested objects are omitted.

**Bad:**
```js
function connectToDatabase(opts) {
const host = opts.host || 'localhost';
const port = opts.port || 80;
const db = opts.db || { name: 'test' };
}
```

**Good:**
```js
function connectToDatabase({
host = 'localhost',
port = 80,
db: { name = 'test' } = {}
} = {}) {
}
```

**[⬆ back to top](#table-of-contents)**

Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp