lundi 9 octobre 2017

Javascript invoking a function in different ways

  1. What is difference / similarities between calling a function like the following ?
  2. What is the purpose of having these two ways?
  3. What are their pros and cons?

The sum function for both this call can be created like the following code

function sum(x) {
    if (arguments.length == 2) {
        return arguments[0] + arguments[1];
    } else {
        return function(y) { return x + y; };
    }
}

console.log(sum(2,3));   // Outputs 5
console.log(sum(2)(3));  // Outputs 5



Aucun commentaire:

Enregistrer un commentaire