Two types of scope :
Strict nested !!!
Rules
function foo(str, a) {
eval( str ); // cheating!
console.log( a, b );
}
var b = 2;
foo( "var b = 3;", 1 ); // 1 3Cheat add new var: b to foo
Similar functions:
var obj = {
a: 1,
b: 2,
c: 3
};
// more "tedious" to repeat "obj"
obj.a = 2;
obj.b = 3;
obj.c = 4;
// "easier" short-hand
with (obj) {
a = 3;
b = 4;
c = 5;
}var obj = {
a: 1,
b: 2,
c: 3
};
// more "tedious" to repeat "obj"
obj.a = 2;
obj.b = 3;
obj.c = 4;
// "easier" short-hand
with (obj) {
d = 6;
}
obj.d === undefined // true
window.d === 6 // truefunction foo() {
var a = 1;
function bar() {
var a = 2;
var b = 3;
console.log('inner a: ', a); // 2
console.log('inner b: ', b); // 3
}
bar();
console.log('a: ', a); // 1
console.log('b: ', b); // ReferenceError
}
foo();