Below code creates a function that captures variables in its parent scope thereby creating a closure. The function is then called after altering the variables in its outer scope:
var p = { a: 3, b: 4 };
var sum1, sum2;
// closure
function add() {
return p.a + p.b;
}
p.a = 2;
sum1 = add();
p = { a: 7, b: 8 };
sum2 = add();
What do you think will be the resulting values of sum1 and sum2 above: