Learning Notes - Node's complement to require

Keywords: node.js

Daily learning notes, including ES6, Promise, Node.js, Webpack, http principles, Vue family bucket, and updates to Typescript, Vue3 and common interview questions may follow.

Looking at the previous article, we already know a little about the basic use of require and how it works.

By learning the source code, we also have the following problems.

Several questions about require

First, let's define two files, one for export and one for import.

// Use require to import files
// a.js
module.exports = "hello";
// b.js
let a = require('./a')
console.log(a); // hello

In the previous article on source analysis, we mentioned this point.

this = module.exports = exports (refer specifically to my previous article) Implementation of require)

So what if we output module.exports == exports here?

The relationship between module.exports, exports, and this

First, let's look at the results of that question above.

// a.js
console.log(module.exports === exports); // true
console.log(module.exports === this , exports === this); // true true

The output is true, and we can also see that all three values are equal because this = module.exports = exports.

Now that the three of them are equal, can I just use exports = hello to output the results?

The answer is No. The reason is also simple, because we define it in the way let exports = module.exports = {}, and the file returns the value of module.exports when it is referenced. We assign exports hello, but module.exports is still empty.

// Roughly speaking, the specific implementation can refer to the source code
function require(){
  let exports = module.exports = {};
  exports = "hello";
  return module.exports;

Now that we know that this assignment will not work because of value references, we can think of it very quickly. Can you use the properties of stack memory to bind them an attribute?

// a.js
exports.a = "hello";
this.a = "hello";
module.exports.a = "hello";
// b.js
let a = require('./a')
console.log(a.a); // hello

We found that all three forms can pass and reference values, which means defining the value of the address in its heap memory and modifying the pointing of the heap value.

Assuming we define both module.exports and attributes, the result will get the value of module.exports, not attributes.

(Note: The value of this cannot be modified directly. Some errors may occur)

// a.js
module.exports.a = "hello";
module.exports = "world";
// b.js
let a = require('./a')
console.log(a); // world

Because module.exports has the highest priority, module.exports will eventually be exported. (require method does not support multiple simultaneous exporting)

Simplification of module.exports

Looking at the definitions above, we can sort out the simplified way module.exports is written.

exports simplification

We now need to export multiple values within the module.

// a.js
module.exports.a = "a";
module.exports.b = "b";
module.exports.c = "c";
// b.js
let result = require('./a')
console.log(); // { a: 'a', b: 'b', c: 'c' }

This can be cumbersome, so we can use exports to simplify it

// a.js
exports.a = "a";
exports.b = "b";
exports.c = "c";
// b.js
// ...  { a: 'a', b: 'b', c: 'c' }

Is to simplify the code.

module.exports simplification

Now that there are several methods to export, we can abbreviate them as follows.

// a.js
module.exports = {
 	// ...

This is an abbreviation of how module.exports combines ES6.

So far, our Node file module and Core Modules of Node That's it.

In the next article, I'll summarize the third-party modules in Node.

This article was created by Mo Xiaoshang. If there are any problems and omissions in this article, you are welcome to correct and communicate.
You can also focus on me Personal Site,Blog Park and Nuggets , I will sync uploads to these platforms after the articles are published.
Finally, thank you for your support!

Posted by rv20 on Sun, 21 Nov 2021 10:56:10 -0800