Require function with another associated function?

Asked

Viewed 99 times

5

In a simple chat project, which I have already used and works 100%, it is used

const express =require('express');//para http
const app = express();//instancia
const http = require ('http').createServer(app);
const io = require('socket.io')(http);

I have already looked for a specific answer, but I still haven’t found the meaning of the application of this http object in parentheses, at ('socket.io')(http).

What do these parentheses mean? They mean an anonymous function associated with require?

I don’t understand how this works and, like, when to use.

2 answers

6


That’s a high order Function, basically is a function that returns a function and executes the returned function.

As we can see in the following example:

function soma(a,b){
  return a+b;
}

function subtracao(a,b){
  return a-b;
}

function multiplicacao(a,b){
  return a*b;
}

function retornaAlgumaOperacao(op){
    switch(op){
      case '+': return soma;
      case '-': return subtracao;
      default: return multiplicacao
    }
}

console.log(retornaAlgumaOperacao('+')(1,1));
console.log(retornaAlgumaOperacao('-')(1,1));
console.log(retornaAlgumaOperacao('*')(1,1));

Note that the function retornaAlgumaOperacao does not know which operation it will return, but knows that it will be a function that will perform an operation with two parameters. When this function is returned, we know that we have to send two parameters to perform the operation.

This increases readability for codes that would perform chained functions, for example.

In the specific case of socket.io, the second parameter serves to return one Server, where the first parameter tells you where the socket will be tied.

  • 1

    Very good explanation. However, every particularity, every exception, confuses us, as in the line ... case '+': Sum Return; ... Here, the 'sum' function is provoked without the parentheses and the necessary arguments and, even so, it gives the message. It’s hard to keep track of all these syntax details.

  • @Maurosimoes over time we get used to the nuances of each language, haha, but basically, when you don’t put the parentheses in the name of a function, javascript understands that it should return the function as a whole. Just as you can assign a function to a name like const minhaFunc = fuction() {} and then just call his name minhaFunc (:

  • you spoke, it is spoken. Who knows, knows; who does not know, ask.

5

These parentheses mean what?

These are call parentheses of a function:

function func1(x){
    //codigo
}

func1(val); //chamar a função func1
//   ^---^---> parenteses de chamada de função

Meaning when you do require('socket.io') gets a function back, and then calls this function by passing the object http as a parameter.

You can see very well that you get a function if you do console.log(require('socket.io'));, that gives you:

function Server(srv, opts){ … }

That is to say get the function Server as a return to the latter require. Then call this function Server and passes object http, being in the function as the parameter srv, the first parameter.

This could also be done in two steps, storing the function first in a variable and then calling:

const funcaoServer = require('socket.io');
const io = funcaoServer(http);
  • and would run the pointed structure by passing a variable, or a string, instead of function to another function?

  • @Maurosimoes I don’t know if I understand the question. You’re asking if you could pass something other than http ?

  • Isac, this is exactly what I asked.

  • 1

    @Maurosimoes will only work with a variable that has the content that function Server wait in the first parameter (srv), which in case has to be an object with certain fields. If you pass any other kind of thing will not work properly. If you do console.log(http) can see the exact structure of the object passed to the function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.