This is how I defined the functions and the variable x,y,z is defined with some integer.
var a = 0;
var x = 1;
var y = 2;
var z = 3;
function f(n) {
a = n;
}
function g(){
console.log(a);
}
function h(){
f(x); g();
}
function k() {
var a = 0; g(); f(y);
}
f(z); g(); k(); g(); h(); g();
The following is my thoughts on if the code above is dynamically scope:
f(z){
a = z; // The value of a became z
}
g(){
console.log(a); // Printing out the value of z
}
k(){
var a = 0;
g(){
console.log(a); // Printing out 0
}
f(y){
a = y; // Assign the value of y to the variable a initialized 5 lines above
}
}
g(){
console.log(a); // Printing out the value of z
}
h(){
f(x){
a = x;
}
g(){
console.log(a) // Printing out the value of x
}
}
g(){
console.log(a) // Printing out value of z or x ??
}
Not sure what the last console.log will output.
See the example snippet with it's comments:
The below example works as you want (if i understood your question). Simply omit
varbeforeain functionk().