Consider the following JavaScript skeletal program: //the main program var X: fu
ID: 3882511 • Letter: C
Question
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 ();Explanation / Answer
1.)
a.) sub1
When it is for static scoping the value of the variable x will remain same or contant and hence, refernecing will become way lot easier than what it is when the main calls sub1.
b.) sub2
When it is for the dynamic scoping the value of the variable x will change as soon as the leading references are working on the given problem statement.
Hence, these are the truth referncing of the variable x and where should it be placed.
2.) The value of the x will be refernced 10 as it is in dynamic scoping because once the function reaches to the next function it will change and the value is reflected as 10 at his last word.
Rate an upvote......Thankyou
Hope this helps......
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.