Home Web Front-end JS Tutorial Detailed explanation of the differences and connections between export default, require and exports, and export

Detailed explanation of the differences and connections between export default, require and exports, and export

Jun 22, 2017 am 11:53 AM
default export

Are you still worried about the differences and connections between module.exports, exports, export and export default, import and require? This article is basically enough!

1. First, let’s figure out a basic question:

module.exports and exports belong to the CommonJS module specification! (Not sure about commonjs? Master, please take a look at the commonjs specifications here)

export and export default belong to ES6 syntax (Not sure about ES6? Master, please take a look at the ES6 module here) !

Similarly import and require belong to ES6 and CommonJS respectively!

2. Now that you know which part of the syntax it belongs to, there is another clear point:

module.exports and exports, export and export default are allExport module;

##import and require are import modules.

#So don’t get confused now, module.exports export corresponds to require import, and export export corresponds to import import.

Hello! Wait, why do we say that module.exports export corresponds to require import, and export export corresponds to import import? Then there are exports and export default! ? Then let's continue.

3. The difference and connection between module.exports and exports

At this point, I have to mention modularity a little bit:

Node applications are composed of modules and adopt the CommonJS module specification.

According to this specification, each file is a module and has its own scope. Variables, functions, and classes defined in a file are private and not visible to other files.

CommonJS specification stipulates that inside each module, the

module variable represents the current module. This variable is an object, and its exports attribute (that is, module.exports) is the external interface. Loading a module actually loads the module.exports attribute of the module.

var x = 5;var addX = function (value) {  return value + x;};module.exports.x = x;module.exports.addX = addX;
Copy after login

The above code outputs the variable

x and function addX through module.exports.

require method is used to load modules.

var example = require('./example.js');console.log(example.x); // 5console.log(example.addX(1)); // 6
Copy after login

After reading the introduction to the commonjs specification just now, you can know the following differences and connections:

In fact, the exports variable points to module.exports, and loading the module actually loads

module.exports of this module. This is equivalent to having a line like this at the head of each module.

var exports = module.exports;
Copy after login

So we can add methods directly to the exports object to represent the external output interface, just like adding it to module.exports. Note that you cannot directly point the exports variable to a value, because this is equivalent to cutting off the connection between exports and module.exports.

3. The difference and connection between export and export default

The module functions are mainly composed of:

export and import constitutes . exportExport the external interface of the module, and the import command imports the interfaces exposed by other modules.

Export is actually a little different from export default in the way they are written. One is to export individual interfaces, and the other is to export an entire interface by default. When using the

import command, the user needs to know the variable name or function name to be loaded, otherwise it cannot be loaded. Here is a simple way to write it without knowing the specific exposed interface names. Just use the export default command to specify the default output for the module.

export can be written like this

<code class=" javascript"><span class="token comment">// testA.js<span class="token keyword">var f <span class="token operator">= <span class="token string">&#39;Miel&#39;<span class="token punctuation">;<span class="token keyword">var name <span class="token operator">= <span class="token string">&#39;Jack&#39;<span class="token punctuation">;<span class="token keyword">var data<span class="token operator">= <span class="token number">1988<span class="token punctuation">;

export <span class="token punctuation">{f<span class="token punctuation">, name<span class="token punctuation">, data<span class="token punctuation">}<span class="token punctuation">;<br/></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

After using the

export command to define the external interface of the module, other JS files can load this through the import command module.

<code class=" javascript"><span class="token comment">// main.js
import <span class="token punctuation">{</span></span></code><code class=" javascript"><span class="token comment"><span class="token keyword"><span class="token operator"><span class="token string"><span class="token punctuation"><span class="token keyword"><span class="token operator"><span class="token string"><span class="token punctuation"><span class="token keyword"><span class="token operator"><span class="token number"><span class="token punctuation"><span class="token punctuation">f<span class="token punctuation">, name<span class="token punctuation">, data</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code><code class=" javascript"><span class="token comment"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation"><span class="token punctuation">} from <span class="token string">&#39;./testA&#39;<span class="token punctuation">;<br/></span></span></span></span></span></span></span></code>
Copy after login

export default can be written like this

<code class=" javascript"><span class="token comment">// export-default.js
export default <span class="token keyword">function <span class="token punctuation">(<span class="token punctuation">) <span class="token punctuation">{
  console<span class="token punctuation">.<span class="token function">log<span class="token punctuation">(<span class="token string">&#39;foo&#39;<span class="token punctuation">)<span class="token punctuation">;<span class="token punctuation">}<br/></span></span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login
// 或者写成function foo() {
  console.log(&#39;foo&#39;);}
export default foo;
Copy after login
<code class=" javascript"><span class="token comment">// import-default.js
import customName from <span class="token string">&#39;./export-default&#39;<span class="token punctuation">;<span class="token function">customName<span class="token punctuation">(<span class="token punctuation">)<span class="token punctuation">;<span class="token comment"> // &#39;foo&#39;<br/><br/></span></span></span></span></span></span></span></span></code>
Copy after login

The following compares the export default and export output.

You can see that the first group is used, the statement does not need to use braces; the second group is used, the corresponding statement needs to use braces, a module can only have one default output, so it can only be used once.


4. The difference and connection between import and require<strong></strong><strong></strong>

It’s already clear after reading the above. import and require are just two syntaxes for importing modules that belong to ES6 and CommonJS respectively.


The above is the detailed content of Detailed explanation of the differences and connections between export default, require and exports, and export. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

'The requested module does not provide an export named' Error appears in Vue Cli - how to solve it? 'The requested module does not provide an export named' Error appears in Vue Cli - how to solve it? Aug 20, 2023 pm 07:25 PM

'Therequestedmoduledoesnotprovideanexportnamed'Error appears in VueCli – how to solve it? During the development of the Vue project, we may encounter the error message 'Therequestedmoduledoesnotprovideanexportnamed'. This error message usually appears when introducing third-party components

Configure default gateway Configure default gateway Dec 07, 2023 pm 03:02 PM

Steps to configure default gateway: 1. Open the control panel; 2. Select Network and Internet; 3. Configure network connection; 4. Configure IP address; 5. Configure DNS server address; 6. Confirm the configuration; 7. Restart the network device. Detailed introduction: 1. Open the Control Panel, in Windows system, click the Start menu, select "Control Panel"; 2. Select Network and Internet, in the Control Panel, select "Network and Internet"; 3. Configure network connections, etc. .

How to configure default gateway How to configure default gateway Dec 07, 2023 am 11:34 AM

Steps to configure default gateway: 1. Determine the IP address of the router; 2. Open the network configuration interface of the computer; 3. Configure network connection; 4. Configure IPv4 settings; 5. Enter the IP address and subnet mask; 6. Configure the default gateway ;7. Configure DNS server; 8. Save settings. Detailed introduction: 1. Determine the router's IP address. The default gateway address is usually the router's IP address. You can find the router's IP address on the back of the router or in the user manual; 2. Open the computer's network configuration, etc.

The difference between export and export default The difference between export and export default Oct 12, 2023 am 10:24 AM

The difference between export and export default is that the export keyword is used to export one or more variables, functions, or classes, while the export default keyword is used to export a default variable, function, or class. In other modules, you can use the import keyword to import these exported variables, functions, or classes.

How to configure default gateway How to configure default gateway Dec 07, 2023 pm 02:56 PM

Steps to configure default gateway: 1. Understand the network environment; 2. Obtain the router IP address; 3. Log in to the router management interface; 4. Find and configure the WAN port settings; 5. Configure the default gateway; 6. Save the settings and exit; 7. Check whether the network connection is normal. Configuring the default gateway is an important step in network setup. It determines which router the host uses to access the Internet.

An in-depth analysis of the default usage of PHP: revealing its secrets for you An in-depth analysis of the default usage of PHP: revealing its secrets for you Mar 23, 2024 am 08:06 AM

PHP is a scripting language widely used in the field of web development. Developers have benefited a lot from its flexibility and powerful functions. In PHP, setting default values ​​is a common operation, and the default keyword plays a crucial role. This article will deeply analyze the usage of default in PHP, reveal its secrets for you, and combine it with specific code examples to help readers better understand. 1. Default parameter values ​​In PHP, we can set default values ​​for the parameters of a function to prevent

Does the Java keyword contain 'default'? Does the Java keyword contain 'default'? Apr 23, 2023 pm 01:13 PM

Is default a java keyword? Answer: default is a keyword in java8, also called "virtualextensionmethods". The Chinese translation is called "virtual extension method", which contains some default usage methods in the interface. When the interface is extended, it will not conflict with the implementation class code related to the interface. Introduction to default and java: 1. The previous interface was a double-edged sword, which could be oriented to abstraction but not oriented to specific programming. 2. The disadvantage of this is that when you need to modify the interface, you must first modify all classes that implement the interface. 3. When you cannot add new methods to the interface, it will not affect the existing display, so the default method is added to solve the problem for everyone.

How to implement MySQL's default constraint default and zero-fill constraint zerofill How to implement MySQL's default constraint default and zero-fill constraint zerofill May 31, 2023 pm 05:10 PM

Default constraint MySQL default value constraint is used to specify the default value of a certain column. Add default constraint method 1: default; method 2: altertable table name modify column name type default default value; createtablet_user10(idint,namevarchar(20),addressvarchar(20)default'Beijing'--specify default constraints);--altertable table name modify column name type default default value; createtablet_user11(idint,namevarchar(20),addr

See all articles