Scope, nested functions, function call

6

I am studying this answer and I came across a doubt in a test I am performing, it 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>

Then my doubts:

a) When calling the function x() everything in it should not be    executed?

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

c) How to change function parameters in the call?

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

    
asked by anonymous 05.01.2017 / 21:37

1 answer

5
  

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

No, when you call the function x within it the y function is defined / declared. z is never defined / declared since y is not invoked / invoked. You would have y() or z() within x , where the scope makes them accessible.

  

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

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

  

c)   How do I change the parameters of the functions in the call?

Just pass values in the arguments. For example: y(1) , or y([123, 456]) . If you use y(1) then the b1 parameter will have the value 1 . This parameter b1 can have a value automatically if b1 is undefined since you have in the declaration: function y(b1 = "b1"){ .

  

d) The error is that the functions were not 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 global scope, they are only available in the scope where they were declared and internal scopes to that.

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);
    
05.01.2017 / 21:45