JavaScript calling a function in different ways

  • What is the difference or similarity between a function call, for example:
  • What is the purpose of these two ways?
  • What are their pros and cons?
  • Can someone explain how this call works -> sum(2)(3); ? And mention another conditional equivalent of this call?

The sum function for both of these calls can be created as 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 
+7
javascript jquery html ecmascript-6 web
source share
3 answers

Depending on the context, both methods are equally important, although I would suggest that the latter form is used less. When to use one above the other? Simple, if you need to pass arguments that are immediately available, use the first form. If one of the arguments does not change and the caller expects a function, use the second form. Consider this code, I believe this clearly illustrates my point:

 function add_direct(a, b) { return a + b; } function add_functor(a) { return function (b) { return a + b; } } var nums = [1,4,8]; var add3 = add_functor(3); console.log(nums.map(add3)); console.log(nums.map(n => { return add_direct(3, n); })); 

Both of them add 3, but the second form makes sense here. Both have their own strengths and weaknesses. Hope this clears the air.

PS: In C ++, the second form is called Functor ; Others call them closing or Function Objects .

+5
source share
  • 1 and 2: Both achieve the same. The difference is that you can use the latter to reduce the size of the code, if and when applicable.
  • 3: Runtime may vary. For example, array mapping slower than a functor if we use the example edge of the Raphael example above.

Huge array - edge case Functor vs Function

  • 4: The discussion of functors usually becomes formal and theoretical. The reason for this is that, like all functional programming methods, functors come from mathematics, in this case category theory. Functors are defined as: "morphisms between categories" i.e. a functor is an object that defines the behavior of " fmap ", which when setting the value and function " morphism " maps the specified function to the value of a certain type " category " and generates a new functor. So the array is a Functor because the array can be displayed on top.

Simply put, a functor is a value (an object in javascript) that:

  • Has a map method that expects a function
  • The expected function can return any value.
  • The map function will return another functor of the same type (as the original functor)

You can find some basic examples here and here .

+4
source share

The first style is a simple and simple function call.

The second style creates a closure around x and returns a function. This function can be assigned to a variable for later use. For example, you can use it to create a family of utility functions with the same logic, but each of them has a different closure:

 function exponentiate(exponent) { return function(number) { return number ** exponent } } var square = exponentiate(2); // creates a square() function var cube = exponentiate(3); // creates a cube() function; console.log(square(2)); console.log(cube(2)); 

Another convenient trick is to create arrays of these functions over sets of DOM nodes (including the node link in the close). This allows you to call the same function in collections of nodes, similar to calling a method, but without changing their prototypes.

+2
source share

All Articles