Skip to content

Comments

「変数のスコープ」の項をリライト#927

Open
nakaterm wants to merge 5 commits intomainfrom
rewrite-variable-scope
Open

「変数のスコープ」の項をリライト#927
nakaterm wants to merge 5 commits intomainfrom
rewrite-variable-scope

Conversation

@nakaterm
Copy link
Contributor

@nakaterm nakaterm commented Feb 15, 2026

resolves #815

@nakaterm nakaterm force-pushed the rewrite-variable-scope branch from bf5fed0 to 1153079 Compare February 15, 2026 10:01
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Feb 15, 2026

Deploying utcode-learn with  Cloudflare Pages  Cloudflare Pages

Latest commit: 5066c13
Status: ✅  Deploy successful!
Preview URL: https://b6200e1c.utcode-learn.pages.dev
Branch Preview URL: https://rewrite-variable-scope.utcode-learn.pages.dev

View logs

@nakaterm nakaterm force-pushed the rewrite-variable-scope branch from 1153079 to 1c4b02e Compare February 15, 2026 10:02

function greet() {
guestCount += 1;
guestCount = guestCount + 1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここで初出の += が出てくるとノイズなので、ここでは普通に今まで扱った方法で書き、下のコラム内で「これを += に書き換えられる」という説明をするようにしています


greet();

// document.write(message); これはエラーになる
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラーになるサンプルコードを提供しない、という方針に従いコメントアウトしている

@nakaterm nakaterm requested review from chvmvd and Copilot and removed request for chvmvd February 15, 2026 11:22
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

「変数のスコープ」セクションを、具体例(関数内のローカル変数/外側で宣言した変数の参照)を用いて説明し直し、合わせて複合代入演算子の説明導線を整理するPRです。

Changes:

  • スコープの説明を、message のローカルスコープ例(関数内のみ参照可)で具体化
  • 外側(関数外)で宣言した変数を関数内から参照・更新できる例を維持しつつ、説明文をリライト
  • 複合代入演算子の説明を「guestCount = guestCount + 1guestCount += 1」の流れに整理

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


```javascript
function greet() {
let message = "Hello!";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここでconstではなく、letを使った理由が分からなかったのですが、何かあったりしますか?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あー普通に忘れてました・・
ただ、下で関数外の変数を参照する例は let でこちらが const なのはノイズなのでどちらかに統一しようとは思います
まあ両方 const にして、+= の話は全部複合代入演算子のコラム内でやるようにしようかなぁ

Copy link
Contributor

@chvmvd chvmvd Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらがconstで、下の例がletになってしまってもそんなに問題ないとは私は思いますが。いい例があれば、そのように統一しても良いと思います。


```javascript
function greet() {
let message = "Hello!";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(optional)

上の説明では、メッセージが"Hello!"ではなく、"Hello World!"になっているため、そちらに統一すると認知負荷が少し小さくなるかもしれません。

```javascript
// 関数を定義しておけば
function greet() {
document.write("Hello World!");
}
// 後から呼び出すことができる
greet();
greet();
```

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確かにそうですね!


```javascript
function greet() {
let message = "Hello!";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同様にletconst


greet();

// document.write(message); これはエラーになる
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(optional)

これを単にコメントアウトすると、document.write(message); これはエラーになるとなってしまい、これも構文エラーになってしまうため、単にコメントアウトしても構文エラーにならないようになっていると少し親切かもしれません。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改行したほうがいいか〜

Copy link
Contributor

@chvmvd chvmvd Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

個人的には次のようにエラーとなるところをコメントアウトせずに書くというのも悪くない気もしてきましたが。

sum = "7"; // Type 'string' is not assignable to type 'number'.
add("3", "4"); // Argument of type 'string' is not assignable to parameter of type 'number'.

https://github.com/mdn/translated-content/blob/7fa1e352b7db8747d8f1050c1d5c4444343047a0/files/ja/web/javascript/guide/grammar_and_types/index.md?plain=1#L132-L135

Copy link
Contributor Author

@nakaterm nakaterm Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あー別にエラーになるコードを書いちゃってる例もあるんですね
ならそれが一番わかりやすそう
(ただまあ今回のはランタイムエラーだし議論の余地はありそう?)

Copy link
Contributor Author

@nakaterm nakaterm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すみません、全体的に例を変えました!


```javascript
guestCount = guestCount + 1;
const siteName = "田中のブログ";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この節の上の例で使われている greetingTypemyName にあたるものを使うことも考えたのですが、そうすると同じユースケースで上では引数で渡しており、こちらでは渡していないということになって混乱を招くので例を変えました。

定数としておいていても変ではないものを探した結果、siteName にしました。

@nakaterm nakaterm requested review from chvmvd and Copilot and removed request for chvmvd February 21, 2026 15:38
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@nakaterm nakaterm requested a review from chvmvd February 21, 2026 15:41
@nakaterm
Copy link
Contributor Author

nakaterm commented Feb 21, 2026

一応 document.write() のコメントアウト外したコミットも作っておきました (賛否分かれそうなので微妙だったら戻しましょう)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

スコープの話の例を追加する

2 participants