无法在函数内获取全局变量 (javascript)

Can't get global variables inside the function (javascript)

var selection = document.getElementById('selection');
var closed = true;

function openorclosebar() {

    if(closed == false){
        selection.style.webkitAnimation='bounceOutDown 1s forwards';
        selection.style.animation='bounceOutDown 1s forwards';
        closed = false;
    }
    else{
        selection.style.webkitAnimation='bounceInUp 1s forwards';
        selection.style.animation='bounceInUp 1s forwards';
        closed = true;
    };
}

如何获取全局变量 "selection" 和 "closed" 以使用它们。我尝试了 "window.selection" 和 "window.closed",但没有任何帮助。如果你有想法,请帮助我,这是一个非常重要的项目。

global closed variable is read-only: It's the windows .closed property - such has happened before with .name:-)

使用 IEFE 使您的变量成为本地变量:

(function() {
    var selection = document.getElementById('selection');
    var closed = true;

    function openorclosebar() {
        if(!closed) {
            selection.style.webkitAnimation='bounceOutDown 1s forwards';
            selection.style.animation='bounceOutDown 1s forwards';
            closed = false;
        } else {
            selection.style.webkitAnimation='bounceInUp 1s forwards';
            selection.style.animation='bounceInUp 1s forwards';
            closed = true;
        }
    }
}());

另请参阅浏览器环境中的 other unsafe names