Why does Typeerror occur?

Asked

Viewed 19 times

1

Hello, I am studying javascript and performing some tests and I would like to understand why this form of call generates error.

function test (){
    function printA(){
        console.log("a");
    }
}
test.printA();

Uncaught TypeError: test.printA is not a function
    at <anonymous>:1:6

function test (){
        function printA(){
            console.log("a");
        }
        printA();
    }
test();
a
  • 1

    Hi Luiz! Take a look at these other questions. If you still have doubts clarifies better here to answer.

  • I thought it was something related to the scope, but I didn’t know how to explain it. Could you see if my understanding is correct? The "printA" function is in the local scope of "test", so it can only be accessed through "test" or assigned to an object, correct?

  • Exact! functions and variables declared within the scope of a function are not accessible outside.

No answers

Browser other questions tagged

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