This code is from YDKJS book part 2 Scope and Clousers, when I run it it gives me an typeError that fn is not a function at bar(); why so?
function foo() {
var a = 2;
function baz() {
console.log(a);
}
fn = baz;
}
function bar(fn) {
fn();
}
foo();
bar();
ILM
2
fn
is the function parameter
you are trying to call undefined
, undefined
is not a function
I have added
var fn;
at the beginning of the code and still get the same result
ILM
4
It’s a question of scope, fn
is the name of the parameter of bar
so when bar
is called fn
takes the value of whatever is passed as argument,
no argument means that fn
is undefined.
This fn
has no relation with the fn
in the global scope
1 Like
system
Closed
5
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.