「3.10 変数のスコープ」

Variable Scope


A global variable has global space; it is defined everywhere in your JavaScript code. On the other hand, variables declared within a function are defined only within the body of the function. They are local variables and have local scope. Function parameters also count as local variables and are defined only within the body of the function.

Within the body of a function, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable:

var scope = "global";     // Declare a global variable
function checkscope() {
    var scope = "local";  // Declare a local variable with the same name
    return scope;         // Return the local value, not the global one
checkscope()              // => "local"

グローバル変数の場合は var 文を省略することができますが、ローカル変数は var 文を宣言しなければなりません。


Function definitions can be nested. Each function has its own local scope, so it is possible to have several nested layers of local scope.

「3.10.1 関数のスコープとホイスティング」

Function Scope and Hoisting

Curiously, this means that variables are even visible before they are declared. This feature of JavaScript is informally known as hoisting: JavaScript code behaves as if all variable declarations in a function (but not any associated assignments) are "hoisted" to the top of the function.

ブロックスコープを持つプログラミング言語では、変数を利用する場所のできるだけ近くで宣言するようにして、スコープをできるだけ狭くすることが、よいプログラミングスタイルだと一般的に言われています。一方で、JavaScript にはブロックスコープがないので、すべての変数を関数の先頭で宣言するようにしているプログラマもいます。このようにすることで、変数の本当のスコープとソースコードが同じになり、コードがわかりやすくなります。

In programming languages with block scope, it is generally good programming practice to declare variables as close as possible to where they are used with the narrowest possible scope. Since JavaScript does not have block scope, some programmers make a point of declaring all their variables at the top of the function, rather than trying to declare them closer to the point at which they are used. This technique makes their source code accurately reflect the true scope of the variables.

「3.10.2 プロパティとしての変数」

Variables As Properties

var truevar = 1;      // A properly declared global variable, nondeletable.
fakevar = 2;          // Creates a deletable property of the global object.
this.fakevar2 = 3;    // This does the same thing.
delete truevar        // => false: variable not deleted
delete fakevar        // => true: variable deleted
delete this.fakevar2  // => true: variable deleted

JavaScript のグローバル変数は、グローバルオブジェクトのプロパティになります。このことは、ECMAScript 仕様で標準化されています。ローカル変数の場合には、このような用件はありません。しかし、ローカル変数についても、関数呼び出しに関連付けられたオブジェクトのプロパティとして扱うこともできます。ECMAScript 3 仕様では、このオブジェクトのことを「Call オブジェクト」と呼んでいます。また、ECMAScript 5 仕様では「Declarative Environment Record」と呼んでいます。

JavaScript global variables are properties of the global object, and this is mandated by the ECMAScript specification. There is no such requirement for local variables, but you can imagine local variables as the properties of an object associated with each function invocation. The ECMAScript 3 specification referred to this object as the "call object", and the ECMAScript 5 specification calls it a "declarative environment record".

  • requirement

JavaScript では、this キーワードを使ってグローバルオブジェクトを参照できます。しかし、ローカル変数が保存されているオブジェクトを参照する方法はありません。

The precise nature of these objects that hold variables is an implementation detail that need not concern us. The notion that these local variable objects exist, is however, is an important one, and it is developed further in the next section.

「3.10.3 スコープチェーン」

The Scope Chain

JavaScript is a lexically scoped language: the scope of a variable can be thought of as the set of source code lines for which the variable is defined. Global variables are defined throughout the program. Local variables are defined throughout the function in which they are declared. and also within any functions nested within that function.


If we think of local variables as properties of some kind of implementation-defined object, then there is another way to think about variable scope. Every chunk of JavaScript code (global code or functions) has a scope chain associated with it. This scope chain is a list or chain of objects that defined the variables that are "in scope" for that code.

トラックバック - http://mnemonic.g.hatena.ne.jp/paragramma/20130314