-
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
Open
nakaterm
wants to merge
5
commits into
main
Choose a base branch
from
rewrite-variable-scope
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
「変数のスコープ」の項をリライト #927
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,61 +104,62 @@ 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>`siteName`を宣言しています。 | ||
|
|
||
| ```javascript | ||
| let guestCount = 0; | ||
|
|
||
| function greet() { | ||
| guestCount += 1; | ||
| document.write("あなたは" + guestCount + "人目のお客様です。"); | ||
| const siteName = "田中のブログ"; | ||
| document.write("ようこそ、" + siteName + "へ!"); | ||
| } | ||
|
|
||
| greet(); // あなたは1人目のお客様です。 | ||
| greet(); // あなたは2人目のお客様です。 | ||
| greet(); // ようこそ、田中のブログへ! と表示される | ||
| ``` | ||
|
|
||
| この例における、`greet`<Term>関数</Term>は、呼び出されるたびに`guestCount`に1を加えています。 | ||
|
|
||
| :::tip[複合代入演算子] | ||
| ここで、<Term>関数</Term>の外側から`siteName`を利用しようとするとエラーになります。 | ||
|
|
||
| [**複合代入演算子**](https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Expressions_and_Operators#%E4%BB%A3%E5%85%A5%E6%BC%94%E7%AE%97%E5%AD%90) は、計算と代入を同時に行うことができる演算子です。 | ||
| ```javascript | ||
| function greet() { | ||
| const siteName = "田中のブログ"; | ||
| document.write("ようこそ、" + siteName + "へ!"); | ||
| } | ||
|
|
||
| `x += y`は、`x = x + y`という意味になります。他にも`-=`や`*=`などの演算子が定義されています。`x -= y`は`x = x - y`、`x *= y`は`x = x * y`という意味になります。 | ||
| greet(); // ようこそ、田中のブログへ! と表示される | ||
|
|
||
| ```javascript | ||
| guestCount += 1; | ||
| document.write(siteName); // これはエラーになる (Uncaught ReferenceError: siteName is not defined) | ||
| ``` | ||
|
|
||
| は以下の文のように読み替えられます。 | ||
| 一方で、<Term>関数</Term>の外側で宣言された<Term>変数</Term>を<Term>関数</Term>の内側から利用することは可能です。 | ||
|
|
||
| ```javascript | ||
| guestCount = guestCount + 1; | ||
| const siteName = "田中のブログ"; | ||
|
|
||
| function greet() { | ||
| document.write("ようこそ、" + siteName + "へ!"); | ||
| } | ||
|
|
||
| greet(); // ようこそ、田中のブログへ! と表示される | ||
| ``` | ||
|
|
||
| ::: | ||
| :::tip[複合代入演算子] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 複合代入演算子を使った例がなくなると、ここで複合代入演算子を説明する必要性もなくなってくるので、複合代入演算子をはじめて使うところに移動してもいいかもです。 |
||
|
|
||
| :::warning[<Term>変数</Term>の<Term>**スコープ**</Term>] | ||
| [**複合代入演算子**](https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Expressions_and_Operators#%E4%BB%A3%E5%85%A5%E6%BC%94%E7%AE%97%E5%AD%90) は、計算と代入を同時に行うことができる演算子です。 | ||
|
|
||
| {/* prettier-ignore */} | ||
| <Term>スコープ</Term>が終わった<Term>変数</Term>は、その時点で破棄されます。 | ||
| `x += y`は、`x = x + y`という意味になります。他にも`-=`や`*=`などの演算子が定義されています。`x -= y`は`x = x - y`、`x *= y`は`x = x * y`という意味になります。 | ||
|
|
||
| 次の例の`greet`<Term>関数</Term>は、呼び出されるたびに`guestCount`に1を加えています。`guestCount += 1;`は、`guestCount = guestCount + 1;`のように読み替えられます。 | ||
|
|
||
| ```javascript | ||
| let outer = 0; | ||
|
|
||
| function increment() { | ||
| let inner = 0; | ||
| outer += 1; | ||
| inner += 1; | ||
| document.write(outer); // 1ずつ増える | ||
| document.write(inner); // 常に1 | ||
| let guestCount = 0; | ||
|
|
||
| function greet() { | ||
| guestCount += 1; | ||
| document.write("あなたは" + guestCount + "人目のお客様です。"); | ||
| } | ||
|
|
||
| increment(); | ||
| increment(); | ||
| greet(); // あなたは1人目のお客様です。 | ||
| greet(); // あなたは2人目のお客様です。 | ||
| ``` | ||
|
|
||
| ::: | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
この節の上の例で使われている
greetingTypeやmyNameにあたるものを使うことも考えたのですが、そうすると同じユースケースで上では引数で渡しており、こちらでは渡していないということになって混乱を招くので例を変えました。定数としておいていても変ではないものを探した結果、
siteNameにしました。