girish
girish

Reputation: 223

difference between function's in javascript

I am new to node.js and I see the 2 function almost every where. Please explain me difference between function greet() & greet(function(){}); with example

So that it can understand clearly.

function greet() {
 this.firstname = 'johan',
 this.lastname = 'Don'
console.log(this.firstname + ' ' + this.lastname);
//...
//...
}


greet(function() {
  this.firstname = 'johan',
 this.lastname = 'Don'
console.log(this.firstname + ' ' + this.lastname);

//...
 //...
});

Please help me with this..

Upvotes: 1

Views: 99

Answers (3)

Nirit Levi
Nirit Levi

Reputation: 251

In your second example the function passed as argument is meaningless (because it never gets executed). However, the syntax in your second example is of Higher Order Functions:functions which gets a callback as an argument.

For example Array.prototype.map is a higher order function. It loops over all cells of a giving array, and apply the given callback function on each item in the array.

let x = ['a','b'];
let y = x.map(function(item,i){return item+i;});
//y = ['a0','b1']

Regarding the 'this' of the callback, it is the object who called it.

Upvotes: 0

Neha Tawar
Neha Tawar

Reputation: 705

A function declaration is made of function keyword, followed by an obligatory function name, a list of parameters in a pair of parenthesis (para1, ..., paramN) and a pair of curly braces {...} that delimits the body code.

  // function declaration
function isEven(num) {  
  return num % 2 === 0;
}
isEven(24); // => true  //Execution of function
isEven(11); // => false

Here you can see the function isEven{..} function declaration that defines isEven function, which determines if a number is even.

Where as greet(function(){}); is nothing but a greet function call with a callback function passed and defined in it once the greet function is executed if you want something to happen after execution that you can handle with callback function for this to work

function greet() {
 this.firstname = 'johan',
 this.lastname = 'Don'
console.log(this.firstname + ' ' + this.lastname);
//...
//...
}

will change as

 function greet(callBack) {//function as parameter received
     this.firstname = 'johan',
     this.lastname = 'Don'
    console.log(this.firstname + ' ' + this.lastname);
    //...
    //...

  callBack()//callback function called 
    }

callback function called will execute the code you have done in callback. refer link for understanding callback

Upvotes: 0

Drag13
Drag13

Reputation: 5978

Ok. Let me try.

The first one is function declaration. This means that we define some named function.

The second one is using function named greet where you pass anonymous function as an argument.

Anonymous function is function which has no name.

Main difference is that in first case you have a function you can call to do the described job. In the second case you pass a function to the outside code and can't control when passed function will be executed.

Hope this helps.

And this is not related to Node.Js but to the pure JavaScript. Additional information can be found here

Upvotes: 1

Related Questions