Purpose of piping in JavaScript?

86 views Asked by At

Doing this apnagent tutorial and I do not understand what is the purpose of 'piping' of setting values in the second line.

var apnagent = require('apnagent')
  , agent = module.exports = new apnagent.Agent(); // <--- WHY this here

Especially I do not understand why module.exports = agent; needs, if earlier in tutorial there is a line like this:

module.exports = "<a1b56d2c 08f621d8 7060da2b c3887246 f17bb200 89a9d44b fb91c7d0 97416b30>"; 

Why does module.exports need to be overwritten?

2

There are 2 answers

0
Buzut On BEST ANSWER

It's not really piping, actually, there's no | (pipe) as in the Unix world.

This pattern makes sure new apnagent.Agent() is accessible in both the local scope with agent and through require via module.exports.

It's exactly the same as doing:

 var agent = new apnagent.Agent();
 module.exports = agent;
0
super2bai On

module.exports can exports your code

new apnagent.Agent(); make a new agent object

agent = module.exports = new apnagent.Agent();

and

module.exports = new apnagent.Agent(); agent = module.exports

they are equals.

Module.exports point to a new object, agent disconnect with the module.exports reference, then through agent = module.exports to re-export module.exports.

Agent and assign it to module.exports so we can access it from all or our different playground scenarios.