javascript - using apply to pass in an object for the this keyword -
I'm trying to teach myself Javascript, but I think it's stuck on a basic basis. My reference book says:
"
This
can be applied to pass in an object to indicate the keyword."
I start with:
var a = 50; Var foo = function () {return.a; }
When I do foo ()
, then I would like 50
then I apply
Try as follows:
foo.apply ({a: "blah"});
When I do foo ()
again, I still get 50
and blah
was not as expected.
It appears that apply
was not successful in
the keyword {a: "blah"}
objects .
I am almost certain that I am misunderstood what can help you with explanation?
apply ()
method does not modify function by any means; It only makes a function call with the specified it
.
You can use bind ()
to create a new function, which always calls the original with the specified this
:
var myFoo = foo.bind ({a: 'blah'}); MyFoo ();
Comments
Post a Comment