@@ -17,52 +17,43 @@ JavaScriptはまた明確な名前空間を持ちません。この事は全て
1717
1818変数が参照されるまでの間、JavaScriptはスコープ全てを遡って参照を探索します。グローバルスコープまで遡っても要求した名前が無いと` ReferenceError ` が発生します。
1919
20- ### The Bane of Global Variables
20+ ### グローバル変数の致命傷
2121
22- // script A
22+ // スクリプト A
2323 foo = '42';
2424
25- // script B
25+ // スクリプト B
2626 var foo = '42'
2727
28- The above two scripts do ** not** have the same effect. Script A defines a
29- variable called ` foo ` in the * global* scope and script B defines a ` foo ` in the
30- * current* scope.
28+ 上記の2つのスクリプトは同じ効果を持って** いません** 。スクリプト Aは` foo ` と呼ばれる変数を、* グローバル* スコープに定義しており、スクリプト Bは` foo ` を* 現在* のスコープで定義ています。
3129
32- Again, that is ** not** at all the * same effect* , not using ` var ` can have major
33- implications.
30+ 再び、` var ` が重大な影響を持っていない、* 同じ効果* では** 無い** スクリプトになります。
3431
35- // global scope
32+ // グローバルスコープ
3633 var foo = 42;
3734 function test() {
38- // local scope
35+ // ローカルスコープ
3936 foo = 21;
4037 }
4138 test();
4239 foo; // 21
4340
44- Leaving out the ` var ` statement inside the function ` test ` will override the
45- value of ` foo ` . While this might not seem like a big deal at first, having
46- thousands of lines of JavaScript and not using ` var ` will introduce horrible and
47- hard to track down bugs.
48-
49- // global scope
50- var items = [ /* some list * /] ;
41+ ` test ` 関数の中の` var ` ステートメントを省略すると` foo ` の値をオーバーライドします。最初の内は大した事ではないように思いますが、JavaScriptが何千行規模になると、` var ` を使っていない事で恐怖とバグの追跡の困難さを招くことになります。
42+
43+ // グローバルスコープ
44+ var items = [/* 同じリスト */];
5145 for(var i = 0; i < 10; i++) {
5246 subLoop();
5347 }
5448
5549 function subLoop() {
56- // scope of subLoop
57- for(i = 0; i < 10; i++) { // missing var statement
58- // do amazing stuff!
50+ // サブループのスコープ
51+ for(i = 0; i < 10; i++) { // varステートメントが無くなった
52+ // 素敵な実装を!
5953 }
6054 }
61-
62- The outer loop will terminate after the first call to ` subLoop ` , since ` subLoop `
63- overwrites the global value of ` i ` . Using a ` var ` for the second ` for ` loop would
64- have easily avoided this error. The ` var ` statement should ** never** be left out
65- unless the * desired effect* is to affect the outer scope.
55+
56+ 外側のループは` subloop ` が最初に呼ばれた後に終了します。なぜなら、` subloop ` がグローバル変数` i ` の値で上書きされているからです。2番目の` for ` ループに` var ` を使用する事によって簡単にこのエラーを回避する事ができます。` var ` ステートメントは* 希望する影響* を外側のスコープに与える場合を除いては、** 絶対** に残してはいけません。
6657
6758### Local Variables
6859
0 commit comments