両方の関数の外に変数win1
を宣言して、両方の関数の変数スコープに入るようにする必要があります。
var win1; // This variable is available in the variable scope of any
// ...functions nested in the current scope.
function f1() { // This function has access to its outer variable scope
win1 = window.open(); // ...so it can access the "win1" variable.
var foo = 'bar'; // This variable is not available in the outer scope
} // ...because it was declared inside this function.
function f2() { // This function has access to its outer variable scope
win1.close(); // ...so it can access the "win1" variable.
var bar = 'baz'; // This variable is not available in the outer scope
// ...because it was declared inside this function.
alert(foo); // Gives a ReferenceError, because "foo" is neither in the
// ...current, nor the outer variable scope.
}
f1(); // Invoke f1, opening the window.
f2(); // Invoke f2, closing the window.
alert(foo); // Would give a ReferenceError, because "foo" is in a nested scope.
「win1」は両方の機能でアクセス可能なスコープになければなりません。 [スコープの詳細について](https://developer.mozilla.org/ja/JavaScript/Reference/Functions_and_function_scope) –
テストケースを見ずに推測することもできます。霊能者ではない。 –