Assume the following JavaScript program was interpreted using static-scoping rules. What value of x is displayed in function sub1? Under dynamic-scoping rules, what value of x is displayed in function sub1? var x; function sub1() { document.write("x = " + x + ""); } function sub2() { var x; x = 10; sub1(); } x = 5; sub2();

Respuesta :

Answer:

The value of x in both scope can be described as follows:

i) Static scope:

x=  5

ii) Dynamic scope:

x=10

Explanation:

The static scoping  is also known as an arrangement, which is used in several language, that defines the variable scope like the variable could be labelled inside the source that it is specified.

i) program:

function sub1()  //defining function sub1

{

  print("x = " + x + ""); //print value

}

function sub2()//defining function sub2

{

   var x; //defining variable x

   x = 10; //assign value in variable x

   sub1();  //caling the function sub1

}

x = 5; //assign the value in variable x

sub2(); //call the function sub2

In Dynamic scoping, this model is don't see, it is a syntactic mode that provides the framework, which is used in several program. It doesn't care how well the code has been written but where it runs.  

ii) Program:

var x //defining variable x

function sub1()  //defining a function sub1

{

print("x = " + x + ""); //print the value of x

}

x = 10; // assign a value in variable x

sub2(); // call the function sub2

function sub2() //defining function sub2

{

var x; //defining variable x

x = 5; // assign value in x

sub1();//call the function sub1

}

In this exercise we have to use the knowledge in computer language to write a code in JAVA, like this:

the code can be found in the attached image

to make it simpler we have that the code will be given by:

  • The first code will be:

unction

sub1 ()    //defining function sub1

{

 print ("x = " + x + ""); //print value}

function

sub2 ()    //defining function sub2

{

 var x;   //defining variable x

 x = 10;   //assign value in variable x

 sub1 ();   //caling the function sub1

}

x = 5;

The secound code will be:

var x    //defining variable x

 function

sub1 ()    //defining a function sub1

{

 print ("x = " + x + ""); //print the value of x

}

x = 10;    // assign a value in variable x

sub2 ();   // call the function sub2

function

sub2 ()    //defining function sub2

{

 var x;   //defining variable x

 x = 5;   // assign value in x

 sub1 ();   //call the function sub1

}

See more about JAVA at brainly.com/question/2266606

Ver imagen lhmarianateixeira
Ver imagen lhmarianateixeira
ACCESS MORE