Friday, January 22, 2010

A little example to figure out Javascript scope

function f1() { var a=1; f2(); }
function f2() { alert(a); }
f1();


function f1() { 
var a=2;
function f2() {
alert(a);
}
f2();
}
f1();


Try it.

2 comments:

  1. haha

    that's intrsting.

    case 2 can alert a because f2 and a are both defined in f1?

    ReplyDelete
  2. fuck, need to input authentication code two times

    ReplyDelete