Receiving the error "is not a function" for a declared function

2

I was writing my js, when I come across the following error:

Uncaught TypeError: xyz is not a function

Released from code (shown simply):

(function () {

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

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

    abc();

})();

As you can see, the function is present right away, so why do I get this error anyway?

    
asked by anonymous 26.07.2016 / 14:41

2 answers

2

You can also use this and call the outer function even though it has the same name.

(function () {

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

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

    abc();

})();
    
26.07.2016 / 14:46
3

After some time breaking the head (believe me), I realized that I am actually overwriting the function because it is using the same name for the variable:

var xyz = xyz();

Changing the variable name in this snippet worked correctly.

It may seem like a stupid mistake, but it kind of took some time, I hope it can help others not to do the same.

    
26.07.2016 / 14:41