Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the disadvantages of module import in JS

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the shortcomings of module import in JS". In daily operation, I believe that many people have doubts about the shortcomings of module import in JS. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what are the shortcomings of module import in JS?" Next, please follow the editor to study!

1. Named import and automatic completion

Suppose I wrote a simple JavaScript module:

/ / stringUtils.js export function equalsIgnoreCase (string1, string2) {return string1.toLowerCase () = string2.toLowerCase ();}

The module stringUtils has exported a function exequalsIgnoreCase that compares two strings that ignore case.

It looks fine. It's fine.

Now import the function exequalsIgnoreCase in app.js file

/ app.js import {equalsIgnoreCase} from'. / stringUtils'; equalsIgnoreCase ('Hello',' hello'); / / = > true

Most of us will introduce:

First, you must write the import name import {}. In this step, IDE cannot provide any suggestions for available names to import.

Then, continue to write from'. / stringUtils', then move back to the curly braces and expand autocomplete to select the name to import.

Although the ES6 module has many advantages, importing the module syntax makes the autocomplete feature difficult to use.

2. Modules in Python

Now let's try to import named components in Python. Does it have the same problem?

The following is the same module stringUtils and function equalsIgnoreCase implemented in Python:

# stringUtils.py def equalsIgnoreCase (string1, string2): return string1.lower () = = string2.lower ()

In Python, it is not necessary to explicitly indicate the function to be exported.

Now, inside another Python module, app, import stringUtils into the equalsIgnoreCase function:

In Python, first indicate where to import the module from: from stringUtils.

If you want to know the functions that can be imported, the editor already knows the module name and gives the necessary advice, which is more friendly.

3. Solution method

The only solution I can find to enable autocomplete for named imports in JavaScript is to call IDE for help.

For example, in Visual Studio Code, you can install the JavaScript (ES6) code snippets plug-in.

When the plug-in is enabled, the cursor first jumps to the location where the module path was written by using the imd code snippet and pressing the tab key. Then, after pressing the tab key, the cursor jumps back to the import location inside the curly braces. It works like this:

At this point, the study on "what are the shortcomings of module import in JS" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report