Shallow or Deep Binding?
A JavaScript implementation of Michael L. Scott's shallow/deep binding tester
JavaScript
/*
* Translation into JavaScript of a code fragment in Michael
* L. Scott's Pragmatics of Programming Languages used to
* illustrate shallow and deep binding.
*/
let x;
function setX(n) {
x = n;
}
function printX() {
console.log(x);
}
function foo(S, P, n) {
let x;
if (n === 1 || n === 3) {
setX(n);
} else {
S(n);
}
if (n === 1 || n === 2) {
printX();
} else {
console.log(x);
P();
}
}
setX(0);
foo(setX, printX, 1);
printX();
setX(0);
foo(setX, printX, 2);
printX();
setX(0);
foo(setX, printX, 3);
printX();
setX(0);
foo(setX, printX, 4);
printX();