Getting "is not a Function" error for a declared function

Asked

Viewed 2,659 times

2

I was writing my js, when I came across the following mistake:

Uncaught TypeError: xyz is not a function

Released from the code (illustrated simply):

(function () {

    function abc() {
        var xyz = xyz();
    }

    function xyz() {
        console.log(123);
    }

    abc();

})();

As you can see, the function is present right away, because then that way I get this error?

2 answers

3

After a while of cracking my head (believe me), I realized that I’m actually overwriting the function by using the same name for the variable:

var xyz = xyz();

Changing the variable name in this chunk, it worked correctly.

It may seem like a stupid mistake, but somehow it took me a while, I hope I can help others not do the same.

3


You can also use the this and call the external function even having the same name.

(function () {

    function abc() {
        var xyz = this.xyz();
    }

    function xyz() {
        console.log(123);
    }

    abc();

})();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.