Consider the following JavaScript program:
var x, y, z;
function sub1() {
var a, y, z;
function sub2() {
var a, b, z;
. . .
}
. . .
}
function sub3() {
var a, x, w;
. . .
}
List all the variables, along with the program units where they are declared, that are visible in the bodies of sub1, sub2, and sub3, assuming static scoping is used.

Relax

Respuesta :

Answer:

Variable       Where Declared

In Sub1 :

        A        Sub1

        Y        Sub1

        Z        Sub1

        X        Main

In Sub2:

        A        Sub2

        B        Sub2

        Z        Sub2

        Y        Sub1

        X        Main

In Sub3 :

        A        Sub3

        X        Sub3

        W       Sub3

        Y        Main

        Z        Main

Explanation:

In static-scoped languages with nested subprograms, the declaration of a variable is checked with the subprogram, if it is not found, it check within the parent method that called it, it continue until it find a declaration, if no declaration is found, it display an error.

In Sub1, a, y, z is declared there while the declaration of x is found in main.

In Sub2, a, b, z is declared, declaration of y is found in sub1 and declaration of x is found in main.

In Sub3, a, x, w is declared while the declaration of y, z is from the main.