According to JavaScript Patterns book (p. 79), this should work:
var ob = {
fn: function foo(m) {alert(m);}
};
fn.apply(ob,['m']);
It doesn’t work.
fn is not defined error thrown.
These 2 work OK:
ob.fn.apply(ob,['m']);
and
ob.fn.apply(null,['m']);
Why doesn’t just fn.apply(ob,['m']) work? Can’t get it.
fndoesn’t work because it’s not a variable in scope. Ifwindow.fnwas defined, orvar fnwas defined elsewhere, then you’d be able to access it asfn.apply. Because neither of those are defined, you need to use the full path to the function on the object:If you want fn to be defined, you could simply set it: