Scope, nested functions, called functions

Asked

Viewed 476 times

6

I’m studying that answer and I came across a doubt in a test that I am performing, follows :

<script>
function x(a1="a1"){          // "x" tem acesso a "a"
	var a2="a2";
	console.log("Consele fx =" + a1);
  	console.log("Consele fx =" + a2);
    function y(b1="b1"){      // "y" tem acesso a "a" e "b"
    	var b2="b2";
        console.log("Consele fy =" + a1);
        console.log("Consele fy =" + a2);
        console.log("Consele fy =" + b1);
        console.log("Consele fy =" + b2);
        function z(c1="c1"){  // "z" tem acesso a "a", "b", e "c"
       		var c2="c2";
            console.log("Consele fz =" + a1);
            console.log("Consele fz =" + a2);
            console.log("Consele fz =" + b1);
            console.log("Consele fz =" + b2);
            console.log("Consele fz =" + c1);
            console.log("Consele fz =" + c2);
        }
   }
}
x(10);
	//Consele fx =10
	//Consele fx =a2
y();//erro
z();//erro
</script>

So my doubts :

to) When calling the function x() everything inside shouldn’t be executed ?

b) How do I y() and z() be executed ?

c) How to change function parameters in the call ?

d) The error that gives is that the functions have not been defined, then as correctly define these function and parameters ?

1 answer

5


to) When calling function x() everything inside should not be executed ?

no, when you call the function x within it is closed/declared function y. z never comes to be closed/declared y is not invoked/called. You would have to have y() or z() inside x, where the scope makes them accessible.

b) How I make y() and z() run ?

What causes a function to be invoked is (). You have to have it in the code y() inside x or z because it is only available in this scope (or within itself). To run z only within y or within itself, that function z is not available to the global scope nor x.

c) How to change function parameters in the call ?

just pass values in the arguments. For example: y(1), or y([123, 456]). If you use y(1) then the parameter b1 will have the value 1. This parameter b1 may have an automatic value if b1be it Undefined once you have in the statement: function y(b1 = "b1"){.

d) The error that gives is that the functions have not been defined, so how to correctly define these functions and parameters ?

they are correctly defined/declared, the problem is of scope as I mentioned in b), they do not exist in the eyes of the global scope, they are only available in the scope where they were declared and internal scopes to this.


function x(a1 = "a1") {
    var a2 = 'a2';

    function y(b1 = "b1") {
        var b2 = "b2";
        console.log("Consele fy =" + a1);
        console.log("Consele fy =" + a2);
        console.log("Consele fy =" + b1);
        console.log("Consele fy =" + b2);

        function z(c1 = "c1") {
            var c2 = "c2";
            console.log("Consele fz =" + a1);
            console.log("Consele fz =" + a2);
            console.log("Consele fz =" + b1);
            console.log("Consele fz =" + b2);
            console.log("Consele fz =" + c1);
            console.log("Consele fz =" + c2);
        }
        z(); // vai assumir "c1" pois o argumento é "undefined"
    }
    y('algo');
}
x(10);

  • 1

    Now I’m starting to put the chopsticks together... VLW man well explained... I’m going to do more tests to deepen myself...

Browser other questions tagged

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