Homework Solution: Consider the following JavaScript skeletal program: //the main program var X: function sub1 () { var x: function sub2() { … } } f…

    CSC 4340 Org. Programming LanguagesFall 2017 1. (50 points) Consider the following JavaScript skeletal program //the main program var X function sub1 0i function sub2(11 function sub3 ) Assume the execution of this program is in the following unit order: main calls subl subl calls sub2 sub2 calls sub3 a. Assuming static scoping, which declaration of x is the correct one for a reference to x in: i. subl ii. sub2 iii. sub3 b. Repeat part a, but assume dynamic scoping 2. (50 points) Assume the following JavaScript program was interpreted using static- scoping rules. What value of x is displayed in function subl? Under dynamic scoping rules, what value of x is displayed in function subl? var X function subl(f />) ; desument..MEAFg(x + + <bE = 羡 functin sub2Of x - 10 subl (); sub2 );
    Consider the following JavaScript skeletal program: //the main program var X: function sub1 () { var x: function sub2() { ... } } function sub3 () { ... } Assume the execution of this program is in the following unit order: main calls sub1 sub1 calls sub2 sub2 calls sub3 a. Assuming static scoping, which declaration of x is the correct one for a reference to x in: i. sub1 ii. sub2 iii. sub3 b. Repeat part a, but assume dynamic scoping. 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 ();

    Expert Answer

     
    1.)

    CSC 4340 Org. Programming LanguagesFall 2017 1. (50 points) Consider the aftercited JavaScript skeletal program //the ocean program var X office sub1 0i office sub2(11 office sub3 ) Pretend the action of this program is in the aftercited ace order: ocean calls subl subl calls sub2 sub2 calls sub3 a. Assuming static scoping, which avowal of x is the rectify individual control a relation to x in: i. subl ii. sub2 iii. sub3 b. Repeat sunder a, barring pretend dynamic scoping 2. (50 points) Pretend the aftercited JavaScript program was interpreted using static- scoping rules. What appreciate of x is displayed in office subl? Under dynamic scoping rules, what appreciate of x is displayed in office subl? var X office subl(f />) ; desument..MEAFg(x + + <bE = 羡 functin sub2Of x - 10 subl (); sub2 );

    Consider the aftercited JavaScript skeletal program: //the ocean program var X: office sub1 () { var x: office sub2() { … } } office sub3 () { … } Pretend the action of this program is in the aftercited ace order: ocean calls sub1 sub1 calls sub2 sub2 calls sub3 a. Assuming static scoping, which avowal of x is the rectify individual control a relation to x in: i. sub1 ii. sub2 iii. sub3 b. Repeat sunder a, barring pretend dynamic scoping. Pretend the aftercited JavaScript program was interpreted using static-scoping rules. What appreciate of x is displayed in office sub1? Under dynamic scoping rules, what appreciate of x is displayed in office sub1? var X office sub1() { muniment.transcribe (“x = ” + x + ” “): } office sub2 () { var x: x = 10: sub1(): } x = 5: sub2 ();

    Expert Exculpation

     

    1.)

    a.) sub1

    When it is control static scoping the appreciate of the substituteable x conciliate reocean identical or contant and hence, refernecing conciliate beseem practice hazard easier than what it is when the ocean calls sub1.

    b.) sub2

    When it is control the dynamic scoping the appreciate of the substituteable x conciliate substitute as quickly as the leading relations are started on the attached height declaration.

    Hence, these are the fact referncing of the substituteable x and where should it be placed.

    2.) The appreciate of the x conciliate be refernced 10 as it is in dynamic scoping consequently once the office reaches to the contiguous office it conciliate substitute and the appreciate is reflected as 10 at his conclusive tidings.