Execution Context

Execution Context

by ZenMaster

JavaScript

function foo(x, y, z) {

    // quantity of defined function arguments (x, y, z)
    console.log(foo.length); // 3
    // quantity of really passed arguments (only x, y)
    console.log(arguments.length); // 2
    // reference of a function to itself
    console.log(arguments.callee === foo); // true
    // parameters sharing
    console.log(x === arguments[0]); // true
    console.log(x); // 10
    arguments[0] = 20;
    console.log(x); // 20
    x = 30;
    console.log(arguments[0]); // 30
    // however, for not passed argument z,
    // related index-property of the arguments
    // object is not shared
    z = 40;
    console.log(arguments[2]); // undefined
    arguments[2] = 50;
    console.log(z); // 40
}

foo(10, 20);
console.log('________________________________________________');
function test(a, b) {
    var c = 10;
    d(); // d
    //e(); // undefined
    function d() { console.log('d'); }
    var e = function _e() {};
    (function x() {});
}

test(10); // call

console.log('________________________________________________');

console.log(x); // function
 
var x = 10;
console.log(x); // 10
 
x = 20;
 
function x() {};
 
console.log(x); // 20

console.log('________________________________________________');

if (true) {
  var aaa = 1;
} else {
  var bbb = 2;
}

console.log(aaa); // 1
console.log(bbb); // undefined, but not "b is not defined"

console.log('________________________________________________');

console.log(aa); // undefined
//console.log(bb); // "bb" is not defined
 
bb = 10;
var aa = 20;

console.log(this);