{"id":277,"date":"2007-12-18T20:54:04","date_gmt":"2007-12-19T01:54:04","guid":{"rendered":"http:\/\/unitstep.net\/blog\/2007\/12\/18\/using-the-call-and-apply-methods-to-change-the-context-of-a-function-in-javascript\/"},"modified":"2007-12-18T20:54:19","modified_gmt":"2007-12-19T01:54:19","slug":"using-the-call-and-apply-methods-to-change-the-context-of-a-function-in-javascript","status":"publish","type":"post","link":"https:\/\/unitstep.net\/blog\/2007\/12\/18\/using-the-call-and-apply-methods-to-change-the-context-of-a-function-in-javascript\/","title":{"rendered":"Using the “call” and “apply” methods to change the context of a function in JavaScript"},"content":{"rendered":"
In JavaScript, the context that function is executing in is very important. What is context? Basically, the context of a function is what object it is “attached” to – this object will be the reference of the “ You may have run into this particular aspect of JavaScript when working with event handlers. For example:<\/p>\n This will return the “id” attribute of the element that the function has been attached to. You could attach the same function to multiple objects and each time you called it on a different object, it would return a different value, since the context is different. (Minor note: using However, there may be times when you want to change the meaning of <\/p>\n Let’s backtrack for a moment. In JavaScript, many (but not all) variables are objects. This includes the references to all functions as well – they are also objects. In the example above, using “ Two of these methods are The values in the second-argument array are then used as the arguments to the The I prefer to use So far, the examples posed have been fairly trivial. Let’s look at a real-world use of We could easily convert it into an array by accessing each property by its index using a loop, but there’s an easier to way to do this. Consider the following code:<\/p>\nthis<\/code>” keyword within that function. Every function has a context -that is to say, every function is attached to an object. By default it is the
window<\/code> object.<\/p>\n
function alertId()\r\n{\r\n alert (this.id);\r\n}\r\n\r\ndocument.getElementById('someId').onclick = alertId;<\/code><\/pre>\n
element.attachEvent()<\/code>, as part of the Microsoft event handler model, does not change the context<\/a> of the function so “
this<\/code>” will still refer to the
window<\/code> object)<\/p>\n
this<\/code> without actually having to attach that a function to an object. Using the
call()<\/code> and
apply()<\/code> methods of the Function object can allow you to do this.<\/p>\n
Context is everything<\/h3>\n
alertId<\/code>” (without<\/em> the parentheses) would refer to the
Function<\/code> object that represents
alertId<\/code>. As the
Function<\/code> object is a predefined object in JavaScript, it has methods and properties associated<\/a> with it.<\/p>\n
call()<\/code> and
apply()<\/code>. Using these methods on a Function object allows you to set the context of that function – that is, what the “
this<\/code>” keyword refers to. The two only in how arguments are passed to the function whose context is being redefined. For example
apply()<\/code> takes an array of arguments like this:<\/p>\n
function someFunction(arg1, arg2)\r\n{\r\n this.id = arg1 + arg2;\r\n}\r\n\r\nvar arrayOfArguments = ['valueOfArg1', 'valueOfArg2'];\r\n\r\nsomeFunction.apply(objectToBeUsedAsThis, arrayOfArguments);<\/code><\/pre>\n
someFunction()<\/code> function. <\/p>\n
call()<\/code> method is almost the same except it does not take an array of arguments, but rather just an indeterminate number of arguments that are all passed to the function. For example, the above could also be accomplished with this code:<\/p>\n
function someFunction(arg1, arg2)\r\n{\r\n this.id = arg1 + arg2;\r\n}\r\n\r\nsomeFunction.call(objectToBeUsedAsThis, 'valueOfArg1', 'valueOfArg2');<\/code><\/pre>\n
apply()<\/code> sometimes since you can directly use the
arguments<\/code><\/a> variable that is local within all functions. (The variable
arguments<\/code> stores all the arguments that were passed to the function when it was called, and so this can be used in a situation with where it is desired to have a function with a variable number of arguments.)<\/p>\n
A useful example<\/h3>\n
call()<\/code> to illustrate its usefulness. Remember the
arguments<\/code><\/a> variable? Basically, you have access to this object variable whenever you’re inside a function since it stores all the arguments passed to this function. However, it’s not exactly an array (although it works like one sometimes) and thus it does not<\/em> have access to
Array<\/code> methods such as
join()<\/code><\/a>. <\/p>\n
function someFunction()\r\n{\r\n var argsArray = Array.prototype.slice.call(arguments, 0);\r\n}<\/code><\/pre>\n