Authored 700th Blog Posts

I am happy and excited to share that I have authored 700 blog posts on http://debugmode.net . Being a reader it is your love and recognition which motivates me to keep writing.

clip_image001

I started writing in November 2008. I wrote my first post on http://www.c-sharpcorner.com/ on 20th November 2008. Since then writing is part of my life. I don’t feel good, if I don’t share on regular basis.

Archives of Posts

It is fun writing for last 5 years. I love my style of writing in which I target to a specific problem and keep post to the point. Thanks again for your love and feedback.

Let us get to 800 blog posts soon . Happy Coding and Debugging .

What is apply() method in JavaScript

Recently I wrote post on What is Bind method in JavaScript and about What is call() method in JavaScript In this post we will take a look on apply method in JavaScript.

apply() method is exactly the same as call() method with minor difference in passing arguments.

Alike call() method using apply() method also, you can invoke a function on given object. Let us understand with some example. Consider that you have a function GreetingMessage() as following ,


function GreetingMessage() {

alert("Hello Geek !");
 };

And you have an object Student as following,

var Student = {};

You can call function GreetingMessage on object Student as following,

GreetingMessage.apply(Student);

As output you should get following output,

clip_image001

Apply() method allows you to invoke function which parameter is array of not fixed length. Apply() method pass arbitrary length array as arguments to function.

Let us consider following example. In below example we are calculating sum of the array in function FindSum().


function FindSum(e) {

var sum = 0;
 for (var i = 0 ; i < arguments.length ; i++) {

sum = sum + arguments[i];

}
 return sum;
 };

var Student = {
 };


var sumOfMarks = FindSum.apply(Student, [12, 45, 11, 56, 189]);
 alert(sumOfMarks);

Essentially above you are passing dynamic array to apply method. In function you can access passed parameters using arguments. As output you will get sum of passed array,

clip_image001[6]

To summarize,

  • apply() method takes at least one parameter.
  • First parameter is object on which function to be invoked
  • Second parameter onwards get passed to the function to be invoked.
  • It passes arbitrary length array to function

This was a brief discussion of apply() method is JavaScript. I hope you find this post useful. Thanks for reading.

What is call() method in JavaScript

Recently I wrote post on What is Bind method in JavaScript . In this post we will take a look on call method in JavaScript.

Using call() method you can invoke a function on given object. Let us understand with some example. Consider that you have a function GreetingMessage() as following ,


function GreetingMessage() {

alert("Hello Geek !");
 };

And you have an object Student as following,


var Student = {};

You can call function GreetingMessage on object Student as following,


GreetingMessage.call(Student);

As output you should get following output,

clip_image001

First argument of call() method is object. After second argument onwards are values to the functions being invoked. So if you have a function as following with parameter.


function GreetingMessage(name) {

alert("Hello "+ name);
 };

var Student = {};

GreetingMessage.call(Student,"dj");

We are passing dj as second argument of call method. So it become parameter to GreetingMessage() function. You will get output of above code snippet as following

image

First argument you pass in call() method can be referred with this operator in function. Consider below example, in this we are accessing property of object with this in function.

 function GreetingMessage() {

alert("Hello "+ this.name);
 };

var Student = {

name :"dj"

};

GreetingMessage.call(Student);

To summarize,

  • Call() method takes at least one parameter.
  • First parameter is object on which function to be invoked
  • Second parameter onwards get passed to the function to be invoked.

This was a brief introduction of call() method is JavaScript. I hope you find this post useful. Thanks for reading.

What is Bind method in JavaScript

In this post we will take a look on Bind function in JavaScript. This method Bind a function to an Object.

For example if you have a function as following,


function FindSquare(number) {

return number * number;
 }

And you have created an object as below,


var Calculator = {};

As you see that calculator object does not contain any properties or method. Now let us say you want to bind function FindSqaure to object Calculator then you can do that using Bind method in JavaScript.

  1. Bind method binds a function to object
  2. It returns a function after binding

So you can bind FindSquare function to Calculator object as below,


var CFindSquare = FindSquare.bind(Calculator);

Now when you call CFindSquare function and pass parameter, it will actually get passed to original function FindSquare.

You can invoke FindSquare method on object Calculator as follows,


var result = CFindSquare(8);
 alert(result);

In this way you can work with bind() method in JavaScript. I hope you find this post useful. Thanks for reading.

Organized C-SharpCorner Delhi Chapter July meet and Presented on JavaScript

Read official recap of July meet here

Once again it was full house in C-SharpCorner Delhi Chapter monthly meet. It has been 4th month in row when chapter monthly meet witnessed more than 100 developers gathered for full day learning.

image

We have been doing monthly chapter meet. Report of last four month is as follows,

clip_image002

Read official recap of July meet here . I got opportunity to present along with Mahesh Chand Brij Mishra and Suchit Khanna

I presented on JavaScript Object and Functions. Find slides from the presentation below,

 

 

In month of August we are planning to do full day event on JavaScript. It will be as JavaScript day.

Join Chapter Facebook Group for all the updates

See you in August with JavaScript