04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5: Reference Types<br />

In this example, callSum1() executes the sum() method, passing in this as the scope (which is equal<br />

to window because it ’ s being called in the global scope) and also passing in the arguments object. The<br />

callSum2() method also calls sum(), but it passes in an array of the arguments instead. Both functions<br />

will execute and return the correct result.<br />

The call() method exhibits the same behavior as apply() , but arguments are passed to it differently.<br />

The first argument is the scope, but the remaining arguments are passed directly into the function. Using<br />

call() , arguments must be enumerated specifically, as in this example:<br />

function sum(num1, num2){<br />

return num1 + num2;<br />

}<br />

function callSum(num1, num2){<br />

return sum.call(this, num1, num2);<br />

}<br />

alert(callSum(10,10)); //20<br />

Using the call() method, callSum() must pass in each of its arguments explicitly. The result is the<br />

same as using apply() . The decision to use either apply() or call() depends solely on the easiest<br />

way for you to pass arguments into the function. If you intend to pass in the arguments object directly<br />

or if you already have an array of data to pass in, then apply() is the better choice; otherwise, call()<br />

may be a more appropriate choice. (If there are no arguments to pass in, these methods are identical.)<br />

The true power of apply() and call() lies not in their ability to pass arguments, but rather in their<br />

ability to augment the scope in which a function runs. Consider the following example:<br />

window.color = “red”;<br />

var o = { color: “blue” };<br />

function sayColor(){<br />

alert(this.color);<br />

}<br />

sayColor();<br />

//red<br />

sayColor.call(this); //red<br />

sayColor.call(window); //red<br />

sayColor.call(o); //blue<br />

This example is a modified version of the one used to illustrate the this object. Once again, sayColor()<br />

is defined as a global function, and when it ’ s called in the global scope, it displays “ red ” because this<br />

.color evaluates to window.color . You can then call the function explicitly in the global scope by<br />

using sayColor.call(this) and sayColor.call(window) , which both display “ red ” . Running<br />

sayColor.call(o) switches the context of the function such that this points to o , resulting in a<br />

display of “ blue ” .<br />

The advantage of using call() (or apply() ) to augment the scope is that the object doesn ’ t need to know<br />

anything about the method. In the first version of this example, the sayColor() function was placed<br />

directly on the object o before it was called; in the updated example, that step is no longer necessary.<br />

129

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!