javascript - Why am I able to assign the result of require to an object? - Stack Overflow

I was curious how the require() function works in Node.js.so I was looking into codes in NodeJS core-mo

I was curious how the require() function works in Node.js.

so I was looking into codes in NodeJS core-modules.

the path of this file in the nodejs project in Webstorm is below.

External Libraries\Node.js Core\core-modules\internal\modules\cjs\loader.js

const {   
       makeRequireFunction,   
       requireDepth,   
       stripBOM,   
       stripShebang } = require('internal/modules/cjs/helpers');

so, I haven't seen above form of variable in javascript. and also I found text in array are the names of functions in helper.js.

helper.js path is below.

External Libraries\Node.js Core\core-modules\internal\modules\cjs\helper.js

// Invoke with makeRequireFunction(module) where |module| is the Module 

object
// to use as the context for the require() function.
function makeRequireFunction(mod) {
  const Module = mod.constructor;

  function require(path) {
    try {
      exports.requireDepth += 1;
      return mod.require(path);
    } finally {
      exports.requireDepth -= 1;
    }
  }

  function resolve(request, options) {
    if (typeof request !== 'string') {
      throw new ERR_INVALID_ARG_TYPE('request', 'string', request);
    }
    return Module._resolveFilename(request, mod, false, options);
  }

  require.resolve = resolve;

  function paths(request) {
    if (typeof request !== 'string') {
      throw new ERR_INVALID_ARG_TYPE('request', 'string', request);
    }
    return Module._resolveLookupPaths(request, mod, true);
  }

  resolve.paths = paths;

  require.main = process.mainModule;

  // Enable support to add extra extension types.
  require.extensions = Module._extensions;

  require.cache = Module._cache;

  return require;
}

I can't think how that variable works even.

I was curious how the require() function works in Node.js.

so I was looking into codes in NodeJS core-modules.

the path of this file in the nodejs project in Webstorm is below.

External Libraries\Node.js Core\core-modules\internal\modules\cjs\loader.js

const {   
       makeRequireFunction,   
       requireDepth,   
       stripBOM,   
       stripShebang } = require('internal/modules/cjs/helpers');

so, I haven't seen above form of variable in javascript. and also I found text in array are the names of functions in helper.js.

helper.js path is below.

External Libraries\Node.js Core\core-modules\internal\modules\cjs\helper.js

// Invoke with makeRequireFunction(module) where |module| is the Module 

object
// to use as the context for the require() function.
function makeRequireFunction(mod) {
  const Module = mod.constructor;

  function require(path) {
    try {
      exports.requireDepth += 1;
      return mod.require(path);
    } finally {
      exports.requireDepth -= 1;
    }
  }

  function resolve(request, options) {
    if (typeof request !== 'string') {
      throw new ERR_INVALID_ARG_TYPE('request', 'string', request);
    }
    return Module._resolveFilename(request, mod, false, options);
  }

  require.resolve = resolve;

  function paths(request) {
    if (typeof request !== 'string') {
      throw new ERR_INVALID_ARG_TYPE('request', 'string', request);
    }
    return Module._resolveLookupPaths(request, mod, true);
  }

  resolve.paths = paths;

  require.main = process.mainModule;

  // Enable support to add extra extension types.
  require.extensions = Module._extensions;

  require.cache = Module._cache;

  return require;
}

I can't think how that variable works even.

Share Improve this question edited Jan 18, 2019 at 14:15 Ruan Mendes 92.4k31 gold badges159 silver badges222 bronze badges asked Jan 18, 2019 at 13:11 NoderNoder 3233 silver badges15 bronze badges 2
  • 1 Could you please improve your title? It should be in the form of a question. Maybe "How can I assign the result of require to an object?" – Ruan Mendes Commented Jan 18, 2019 at 13:31
  • @JuanMendes ok. I was considering how to make title for a while. and thanks for your advice. :) – Noder Commented Jan 18, 2019 at 13:43
Add a ment  | 

2 Answers 2

Reset to default 8

That's called Object Destructuring. The require returns an object containing several keys (including the ones in your example) and ES6+ javascript will make each of those keys available as a direct constant

Example:

// object containing name, country & job keys
const person = {name: "John Doe", country: "Belgium", job: "Developer"};

// destructuring assignment, grabbing name, country & job from the person object
const {name, country, job} = person;

console.log(name);//"John Doe"
console.log(country);//"Belgium"
console.log(job);//"Developer"

Note that you can also assign a different variable name with a similar syntax.
Given the previous object:

const {job: occupation} = person

console.log(occupation); //"Developer"

require in Node parses the JavaScript file and returns a window.exports object which is created by wrapping some code around the original JS. See What does require() actually return, the file or the function and https://nodejs/api/modules.html#modules_require_id

Extra resources: MDN resource

This is called Object Destructuring, and assigns the values by 'matching' on the return value (either an object or an array).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745663173a4638960.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信