-
Notifications
You must be signed in to change notification settings - Fork 3
「変数のスコープ」の項をリライト #927
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
「変数のスコープ」の項をリライト #927
Changes from 3 commits
1c4b02e
e615ec5
a5ef48a
37c6674
5066c13
e32d573
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -104,61 +104,61 @@ document.write(multiply(3, 4)); | |||||||||||||||||||||
| ## <Term>変数</Term>の<Term>スコープ</Term> | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| {/* prettier-ignore */} | ||||||||||||||||||||||
| <Term>関数</Term>内で<Term>宣言</Term>された<Term>変数</Term>は、<Term>関数</Term>内でのみ有効です。<Term>変数</Term>が有効な範囲のことを、その<Term>変数</Term>の<Term>**スコープ**</Term>と呼んでいます。 | ||||||||||||||||||||||
| <Term>関数</Term>やif文などの中で`let`や`const`を使って宣言された<Term>変数</Term>は、それらの内部でのみ有効です。<Term>変数</Term>が有効な範囲のことを、その<Term>変数</Term>の<Term>**スコープ**</Term>と呼んでいます。 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| {/* prettier-ignore */} | ||||||||||||||||||||||
| <Term>関数</Term>外で<Term>宣言</Term>された<Term>変数</Term>は<Term>関数</Term>内でも利用できます。 | ||||||||||||||||||||||
| 次の例では、<Term>関数</Term>`greet`の中で<Term>変数</Term>`message`を宣言しています。 | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ```javascript | ||||||||||||||||||||||
| function greet() { | ||||||||||||||||||||||
| let message = "Hello!"; | ||||||||||||||||||||||
|
||||||||||||||||||||||
| ```javascript | |
| // 関数を定義しておけば | |
| function greet() { | |
| document.write("Hello World!"); | |
| } | |
| // 後から呼び出すことができる | |
| greet(); | |
| greet(); | |
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確かにそうですね!
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同様にlet → const?
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
エラーになるサンプルコードを提供しない、という方針に従いコメントアウトしている
nakaterm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(optional)
これを単にコメントアウトすると、document.write(message); これはエラーになるとなってしまい、これも構文エラーになってしまうため、単にコメントアウトしても構文エラーにならないようになっていると少し親切かもしれません。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改行したほうがいいか〜
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
個人的には次のようにエラーとなるところをコメントアウトせずに書くというのも悪くない気もしてきましたが。
utcode-learn/docs/4-advanced/03-typescript/index.mdx
Lines 102 to 104 in c8ad394
| 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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あー別にエラーになるコードを書いちゃってる例もあるんですね
ならそれが一番わかりやすそう
(ただまあ今回のはランタイムエラーだし議論の余地はありそう?)
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここで初出の += が出てくるとノイズなので、ここでは普通に今まで扱った方法で書き、下のコラム内で「これを += に書き換えられる」という説明をするようにしています
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
複合代入演算子を使った例がなくなると、ここで複合代入演算子を説明する必要性もなくなってくるので、複合代入演算子をはじめて使うところに移動してもいいかもです。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここで
constではなく、letを使った理由が分からなかったのですが、何かあったりしますか?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あー普通に忘れてました・・
ただ、下で関数外の変数を参照する例は
letでこちらがconstなのはノイズなのでどちらかに統一しようとは思いますまあ両方
constにして、+=の話は全部複合代入演算子のコラム内でやるようにしようかなぁUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
こちらが
constで、下の例がletになってしまってもそんなに問題ないとは私は思いますが。いい例があれば、そのように統一しても良いと思います。