最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin7PV0评论

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).

Post a comment

comment list (0)

  1. No comments so far