Tuesday, August 26, 2014

with scope, const and var.

Consider the following functions:

function foo() {
  var o = { a: 1 };
  with (o) {
    var a = 2;
  }
  return(o.a);


function bar() {
  var o = { a: 1 };
  with (o) {
    const a = 2;
  }
  return(o.a);
}

Eagle-eyed readers will have noticed that the only difference between the functions foo and bar is the variable declaration inside the with scope.

foo() returns 2.
bar() returns 1.

No comments:

Post a Comment