2

I have the following Javascript file:

function availableHeight() {
    var windowHeight = window.innerHeight;
    var headerHeight = $(".page-header").innerHeight();
    var marginPaddingEtc = 105;
    return windowHeight - marginPaddingEtc - headerHeight;
}

function setMapHeight() {
    availableHeight = availableHeight();
    $("#map").outerHeight(availableHeight);
}

which is named utils.js and is placed inside a js/ folder relative to my HTML file. In the HTML file, I import utils.js like:

<script type="text/javascript" src="js/utils.js"></script>

and use the functions at the end of the body like:

<script>
$(document).ready(function () {
    $("#menu").load("demo_nav.html", function() {
        setMapHeight();
        var availableHeight = availableHeight();
        console.log(availableHeight);
    });
});
</script>

When opening the page in Firefox 44.0.2, I get the following output in the console:

TypeError: availableHeight is not a function


var availableHeight = availableHeight();

What surprises me the most is the fact that setMapHeight() is being found and availableHeight() is not, even though they are in the same file! Since one is found, I know the issue is not with the import. I am calling the functions in $(document).ready() so everything should be loaded by then. I tried renaming the var just in case the declaration was replacing the function, but that didn't solve it either.

I am out of ideas of why this is not working. Can you find anything wrong?

EDIT: ANSWER by Dmitriy Loskutov

setMapHeight() was redefining the global name because it didn't declare its variable with var. I changed it to

function setMapHeight() {
    var myAvailableHeight = availableHeight();
    $("#map").outerHeight(myAvailableHeight);
}

and the error went away.

Thanks

1 Answer 1

3

The local availableHeight variable is hoisted to the top of its function, shadowing the global one.

Before you assign some value, it has the default undefined value. But undefined is not a function, so you can't call it.

Your code becomes something like

function availableHeight() { /* ... */ }
(function () {
  var availableHeight = undefined; // Local variable hoisted to the top
  availableHeight = availableHeight(); // TypeError (can't call undefined)
})();
Sign up to request clarification or add additional context in comments.

6 Comments

This would be fixed by renaming the local variable, correct? I already tried changing the name of the local var and it didn't fix it :(
Renaming doesn't fix it. What else could it be? TypeError: availableHeight is not a function var somethingElse = availableHeight();
@gamda Are you sure there is no other availableHeight somewhere else? What happens if you use console.log(availableHeight) in that function and just after the external script?
check your setMapHeight function in utils.js, you redefine global variable availableHeight too
@DmitriyLoskutov thank you, you were right. Changing the name inside setMapHeight worked!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.