グローバル変数はグローバル変数に格納されwindow
ます。次のコードは、関数などの外部で宣言するだけで機能します。
var test="stuff";
console.log(window.test);
同様の証拠はそれwindow.location.href
がと同じであるということですlocation.href
ただし、問題は変数が宣言された場所にある可能性があります。たとえば、この変数を関数で宣言した場合、この変数は関数にのみ存在し、グローバルには存在しません。
function foo(){
//declaring inside function
var test="stuff";
//undefined, since the variable exists in the function only
console.log(window.test);
//undefined, document refers to the document
//which is the top DOM object, not the global window
console.log(document.test);
//depends on what "this" refers to, especially
//when using call() or apply() to call the function
//For normal function calls, usually it points to window as well
console.log(this.test);
//none of the above refer to "test" that contains "stuff"
//because you are looking in the wrong place
}