I have a pretty good understanding of Javascript, except that I can't figure out a nice way to set the "this" variable. Consider:
var myFunction = function(){
alert(this.foo_variable);
}
var someObj = document.body; //using body as example object
someObj.foo_variable = "hi"; //set foo_variable so it alerts
var old_fn = someObj.fn; //store old value
someObj.fn = myFunction; //bind to someObj so "this" keyword works
someObj.fn();
someObj.fn = old_fn; //restore old value
Is there a way to do this without the last 4 lines? It's rather annoying... I've tried binding an anonymous function, which I thought was beautiful and clever, but to no avail:
var myFunction = function(){
alert(this.foo_variable);
}
var someObj = document.body; //using body as example object
someObj.foo_variable = "hi"; //set foo_variable so it alerts
someObj.(function(){ fn(); })(); //fail.
Obviously, passing the variable into myFunction is an option... but that's not the point of this question.
Thanks.
If you want to 'store' the
this
value to a function so that you can call it seamlessly later (e.g. when you don't have access to that value anymore), you canbind
it (not available in all browsers though):There are two methods defined for all functions in JavaScript,
call()
, andapply()
. The function syntax looks like:What these functions do is call the function they were invoked on, assigning the value of the object parameter to this.
I think you're looking for
call
:This calls
myFunction
withthis
set toobj
.There is also the slightly different method
apply
, which takes the function parameters as an array:Setting the
this
keyword in javascript.Javascript has 3 built in methods for setting the
this
keyword conveniently. They are all located on theFunction.prototype
object so every function can use them (since every function inherits from this prototype via prototypal inheritance). These functions are the following:Function.prototype.call()
: This function takes the object which you want to use asthis
as a first argument. Then the remainder of the arguments are the respective arguments of the function which is called.Function.prototype.apply()
: This function takes the object which you want to use asthis
as a first argument. Then the second argument is an array which contains the values of the arguments of the function which is called (first element of array is first argument of the function, second argument of the array is second argument of function etc.).Function.prototype.bind()
: This function returns a new function which has a different value ofthis
. It takes the object which you want to set as thethis
value as a first argument and then returns a new function object.Difference between call/apply and bind:
call
andapply
are similar in the fact that they immediately call the function (with a predefined value ofthis
)bind
is different fromcall
andapply
in the fact that this function returns a new function with a different binding of thethis
value.Examples:
My search on how to bind
this
brought me here so I am posting my findings: In 'ECMAScript 2015' we can also set this lexically using arrow functions to.See: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions
Instead of:
We can now do: