


Detailed explanation of the differences and connections between export default, require and exports, and 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
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;
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
module.exports of this module. This is equivalent to having a line like this at the head of each module.
var exports = 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.
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.
<code class=" javascript"><span class="token comment">// testA.js<span class="token keyword">var f <span class="token operator">= <span class="token string">'Miel'<span class="token punctuation">;<span class="token keyword">var name <span class="token operator">= <span class="token string">'Jack'<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>
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">'./testA'<span class="token punctuation">;<br/></span></span></span></span></span></span></span></code>
<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">'foo'<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>
// 或者写成function foo() { console.log('foo');} export default foo;
<code class=" javascript"><span class="token comment">// import-default.js import customName from <span class="token string">'./export-default'<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"> // 'foo'<br/><br/></span></span></span></span></span></span></span></span></code>
4. The difference and connection between import and require<strong></strong><strong></strong>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



'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

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

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

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.

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

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.

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
