0

This isn't really a code-heavy question since it's more of a concept-type.

var args = require('minimist')(process.argv.slice(2), {string: "name"});

How does the code above work? I understand that I am including the minimist library in from the NPM but I don't quite understand why there's (process.argv.slice(2)). There are two open close parentheses over them.

I don't know how this process is called in Javascript. Is there any name for this form of usage ('minimist')(process.argv.slice....)?

1
  • The require statement returns the module.exports defined in the "minimist" module, which is a function, and the parentheses applies the function with 2 parameters. The return value is stored in the "args" variable. Commented May 21, 2016 at 13:22

1 Answer 1

2

Your code is equivalent to:

var minimist = require('minimist');
var args = minimist(process.argv.slice(2), {string: "name"});

This means, the second parenthesis of your code is actually calling minimist (or rather the function exported by the minimist module) with two arguments:

  1. process.argv.slice(2): all the arguments from the command line
  2. {string: "name"}: The options object

I'm not aware of any official name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.