Home Web Front-end JS Tutorial Introduction and export of commonJS and es6 specifications

Introduction and export of commonJS and es6 specifications

Mar 10, 2018 pm 02:05 PM
javascript Export

This time I will bring you the introduction and export of commonJS and es6 specifications. What are the precautions for the introduction and export of commonJS and es6 specifications. The following is a practical case, let's take a look.

Definition in index.js:

var info = { name = 'sisi' };
Copy after login

1.When defining variables, do not use objectvariable name, as unknown errors may occur. .

2. When using export {info} for output, the import must be in the form of import {info} from index.js, and the curly brackets cannot be omitted.

3. When the output is in the form of export{info as vm}, the info interface can no longer be used for import, and only the vm interface can be used, that is,

import {vm} from index.js
Copy after login

4. Key points: The output uses module.exports = info When introducing info, you can also use import.

import info from index.js
Copy after login

cannot be used

import {info} from index.js
Copy after login

Of course, in this case, use require ;Also correct

var info = require('index.js');
Copy after login

5. Compare with the fourth item above, if the output uses es6 export. That is, export {info}; or export default info; then the module must use import to introduce info, and require will not work.

6. When using export output, the import must have {}, for example, demo.js

export const str = 'sisi';
export function func(){
   console.log('sisi');
}
Copy after login

must be imported with:

import {str} from 'demo'; 或 import {str, func} from 'demo';
Copy after login

cannot be used

export default const str = 'sisi';
Copy after login
Copy after login

But when using

export default const str = 'sisi';
Copy after login
Copy after login

to import, you can use

import str from 'demo';
Copy after login

because there can only be one export default

es6 {
  export   :      '可以输出多个,输出方式为 {}' ,
  export  default : ' 只能输出一个 ,可以与export 同时输出,但是不建议这么做',
  解析阶段确定对外输出的接口,解析阶段生成接口,
  模块不是对象,加载的不是对象,
  可以单独加载其中的某个接口(方法),
  静态分析,动态引用,输出的是值的引用,值改变,引用也改变,即原来模块中的值改变则该加载的值也改变,
  this 指向undefined
}
commonJS {
  module.exports =  ...   :      '只能输出一个,且后面的会覆盖上面的' ,
  exports. ...  : ' 可以输出多个',
  运行阶段确定接口,运行时才会加载模块,
  模块是对象,加载的是该对象,
  加载的是整个模块,即将所有的接口全部加载进来,
  输出是值的拷贝,即原来模块中的值改变不会影响已经加载的该值,
  this 指向当前模块
}
Copy after login

in a file or module. I believe I read the case in this article. You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

Entry, output, module analysis of webpack3.x

##Vue2 tab switching method

The above is the detailed content of Introduction and export of commonJS and es6 specifications. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

How to get Douyin private message emoticons on WeChat? How to export the private message emoticon package? How to get Douyin private message emoticons on WeChat? How to export the private message emoticon package? Mar 21, 2024 pm 10:01 PM

With the continuous rise of social media, Douyin, as a popular short video platform, has attracted a large number of users. On Douyin, users can not only show their lives but also interact with other users. In this interaction, emoticons have gradually become an important way for users to express their emotions. 1. How to get Douyin private message emoticons on WeChat? First of all, to get private message emoticons on the Douyin platform, you need to log in to your Douyin account, then browse and select the emoticons you like. You can choose to send them to friends or collect them yourself. After receiving the emoticon package on Douyin, you can long press the emoticon package through the private message interface, and then select the "Add to Emoticon" function. In this way, you can add this emoticon package to Douyin’s emoticon library. 3. Next, we need to add the words in the Douyin emoticon library

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Exporting songs downloaded from QQ Music as mp3 on mac only requires two steps Exporting songs downloaded from QQ Music as mp3 on mac only requires two steps Jan 05, 2024 pm 07:10 PM

Listening to music is a very common thing, I believe many friends will do it no matter where they are. What software do you usually use to listen to music? Do you use QQ Music like me? I currently use QQ Music to listen to songs, and it can be used not only on mobile phones, but also on Mac computers. In addition to listening to songs online, we can also download our favorite songs from QQ Music to the computer. However, the songs downloaded from QQ Music for Mac are not in the format we need. What we need is music in MP3 format. So how to export the songs downloaded from QQ Music for Mac to MP3 format? How to export and convert songs downloaded from QQ Music for Mac to MP3 format? If you want to export and convert songs downloaded from QQ Music for Mac to MP

How to export xmind files to pdf files How to export xmind files to pdf files Mar 20, 2024 am 10:30 AM

xmind is a very practical mind mapping software. It is a map form made using people's thinking and inspiration. After we create the xmind file, we usually convert it into a pdf file format to facilitate everyone's dissemination and use. Then How to export xmind files to pdf files? Below are the specific steps for your reference. 1. First, let’s demonstrate how to export the mind map to a PDF document. Select the [File]-[Export] function button. 2. Select [PDF document] in the newly appeared interface and click the [Next] button. 3. Select settings in the export interface: paper size, orientation, resolution and document storage location. After completing the settings, click the [Finish] button. 4. If you click the [Finish] button

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to export the cross-section diagram in Kujiale_How to export the cross-section diagram in Kujiale How to export the cross-section diagram in Kujiale_How to export the cross-section diagram in Kujiale Apr 02, 2024 pm 06:01 PM

1. First, open the design plan to be processed in Kujiale and click on the construction drawings under the drawing list above. 2. Then click to select the full-color floor plan. 3. Then hide the unnecessary furniture in the drawing, leaving only the furniture that needs to be exported. 4. Finally, click Download.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

See all articles