简体   繁体   中英

Why am I able to assign the result of require to an object?

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.

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.org/api/modules.html#modules_require_id

Extra resources: MDN resource

这称为对象解构 ,并通过返回值(对象或数组)上的“匹配”来分配值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM