函数没有在javascript中调用

时间:2015-07-26 12:48:17

标签: javascript

我有以下代码:

function isVisible(selector) {
    $(selector).is(':visible');
}

var isMapVisible;
var initialWindowWidth = window.innerWidth;
/* Window Resize Helper */
function handleWindowResize() {
    isMapVisible = isVisible('.search__map');
    if (window.innerWidth > 768) {
        var windowHeight = window.innerHeight,
        searchResultsHeight = windowHeight - $('.search__results').position().top,
        searchMapHeight = windowHeight - $('.search__map').position().top;
        $('.search__results').height(searchResultsHeight);
        $('.search__map').height(searchMapHeight);
        if (initialWindowWidth < 769) {
            /* 
                This is a hack to trigger the map to show up if initial windowWidth causes the map to hide. 
                it only triggers once.
            */
            resizeMap();
            map.setZoom(map.getZoom() - 1);
            initialWindowWidth = 1000;
        }
    } else {
        $('.search__results').height('auto');
    }
}

在函数handleWindowResize()中,我有一个变量,我将其全局设置为isMapVisible。并且正在调用另一个函数isVisible()。我发现当我用isMapVisible = $('.search__map').is(':visible')替换那行代码时,我能够得到正确的结果,但是如果我的代码如上所复制,我将得到未定义。知道为什么吗?

1 个答案:

答案 0 :(得分:1)

那是因为你没有在你的函数中返回任何东西:

function isVisible(selector) {
    return $(selector).is(':visible');
}