my code :
var n;
function f(){
var v = "kevin";
n = function(){
return v;
}
}
execute in FireBug:
n();
the result is “kevin”
execute in Chrome & IE9:
document.writeln(n); ======>show “undefine”
document.writeln(n()); ======>show nothing
I want to know what exactly the brows doing when execute the code.
Thanks.
That code is a demo of the book “Object Oriented JavaScript”, Chapter 3, Closure 2#
The variable
nis not given a value (i.e., is not assigned to that function) until the functionf()has been executed – which doesn’t happen in the code you show.So
document.writeln(n);should show “undefined”, whiledocument.writeln(n());should be an error since n is not a function.I don’t know why it works in FireBug – have you already executed
f()when you try it?