javascript - what does fn.apply(fn, []) do? -
i have piece of code accepts fn
argument , stores in object
property.
var obj = {}; function anotherfn(fn){ obj["name"] = fn.apply(fn, []); } function google(){ console.log("hello"); } anotherfn(google); console.log(obj.name);
what not understanding fn.apply(fn,[])
code , purpose. use call
, apply
method when want execute fn
in different this/context
.
but fn.apply(fn, [])
here?. confusion why can't do
obj["name"] = fn();
fn.apply(fn, [])
calls function stored in fn
context (the value of this
while executing function) of fn
, , arguments contained within []
(no arguments).
it seems odd call apply
in way, when have been equivalent call fn.call(fn)
.
fn()
not appropriate replacement, fn()
execute in global context, means value of this
within function window
(assuming browser environment).
Comments
Post a Comment