Thursday, August 4, 2011

Javascript : Call Vs Apply (Part 2)

This is part 2 of JavaScript Call Vs Apply.

Part 1 explained bit about of switching context in JavaScript and its uses. This part explains how to use Call and Apply for this and what is the difference between Call and Apply.

The only difference between Call and Apply is in how they handle the arguments. In Call you need to pass the arguments (as you do for normal function calls). In Apply you need to pass the arguments as an array. To see this in action, I am going to modify the function used in Part 1 by adding additional arguments:

function displayDetails(argOne, argTwo)
{
          alert(argOne + ‘:’ + argTwo);
}

Following is the syntax to invoke the above function using Call and Apply:

displayDetails.call(object, ‘one’, ’two’);
displayDetails.apply(object, [‘one’,’two’]);

As you could see from above, the only difference is the arguments in Call are sent separately but in Apply it is sent as an array.

One scenario where Apply could be preferred over Call is when you need to pass all the arguments of the parent function to the target function. For example, consider the following code:

function test(testOne, testTwo)
{
          displayDetails.call(this, testOne, testTwo); //works as expected.
displayDetails.apply(this, arguments); //works as expected.
}

Both the lines in the above function work correctly. It you are going to add another parameter to “test” function, then you would need to modify the first line (Call) but not the second (Apply).

function test(testOne, testTwo, testThree)
{
          displayDetails.call(this, testOne, testTwo, testThree); //need to add the additional parameter.
displayDetails.apply(this, arguments); //no changes required.
}

Hope this post was useful.

Happy coding!!

Tuesday, July 12, 2011

Javascript : Call Vs Apply (Part 1)

I have been using Javascript extensively for quiet some time. It is almost impossible for me to write a Javascript app without using either of the two (Call/Apply).IMHO, these are very powerful functions in Javascript.

Both does the same (i.e. change the context) but with subtle difference in terms of how they handle the arguments. But first, why do you need to change the context of a function and what is it exactly??

Ok, now consider the following two functions (or objects as all functions in Javascript are actually objects)

function Dolphin()
{
 this.details = "I am a Mammal!!";

 this.canSwim = "Yes";
 this.canFly = "No";
}


function Shark()
{
  this.details = "I am a Fish!!";

  this.canSwim = "Yes";
  this.canFly = "No";
}


Now instantiate these objects:
var d = new Dolphin();
var s = new Shark();


If I want to add a method to display the details of these objects then we would write a generic function and pass the details as arguments as shown below:

function displayDetails(details)
{
 alert(details);
}


displayDetails(d.details); //this displays "I am a Mammal!!"
displayDetails(s.details); //this displays "I am a Fish!!"

But what if "displayDetails" needs to be extended to show other details (like "can fly", "can swim" etc)? Then we have to add more arguments to "displayDetails" as below:

function displayDetails(details, canSwim, canFly)
{
 alert(details + ':' + canSwim + ':' + canFly);
}


and pass the additional arguments:

displayDetails(d.details, d.canSwim, d.canFly); //this displays "I am a Mammal!!:Yes:No"
displayDetails(s.details, s.canSwim, s.canFly); //this displays "I am a Fish!!:Yes:No"

As you can see from above code, it is more prudent to pass the entire object to "displayDetails" as below:

function displayDetails(animal)
{
 alert(animal.details + ':' + animal.canSwim + ':' + animal.canFly);
}


and call this as follows:

displayDetails(d); //this displays "I am a Mammal!!:Yes:No"
displayDetails(s); //this displays "I am a Fish!!:Yes:No"

Won't it be nice if we can call "displayDetails" as a function of the object Dolphin or Shark so that we could invoke "displayDetails" as:

d.displayDetails();
s.displayDetails();


call/apply functions does the same thing but with different syntax:

displayDetails.call(d); //this displays "I am a Mammal!!:Yes:No"
displayDetails.call(s); //this displays "I am a Fish!!:Yes:No"


But we need to make couple of changes (remove param and use "this" instead of param) to displayDetails as below:

function displayDetails()
{
 alert(this.details + ':' + this.canSwim + ':' + this.canFly);
}


As you can see from above, we are changing what "this" means inside the function "displayDetails". When a "Dolphin" object is passed, "this" refers to "Dolphin" object. When "Shark" object is passed, "this" refers to "Shark" object. What we have done is changed the context in which "displayDetails" executes.

It has been rather long blog than I anticipated. So I will move the remaining to Part 2.

Happy coding!!