Thoughts on javascript module loading technology _ javascript tips-js tutorial

Source: Internet
Author: User
This article mainly introduces some ideas about the javascript module loading technology. If you need it, you can refer to a user who asked me how to use requireJs and seajs on the front end, I asked him if your company had a self-written javascript library or javascript framework before. His answer was nothing. He just heard that requirejs and seajs are new technologies, it is very valuable, so I want to use it.

This Netizen's problem caused me to think about the javascript module loading technology. In the previous article, I gave myself the basic structure of a javascript library, in fact, one of the reasons for writing this article is that I want to use technologies such as requirejs or seajs to re-design the basic model of my javascript library. When I have a deep understanding of this technology, I found that it is incorrect to use the module loading system to solve the problem of decoupling general code from business code in the javascript library, the function of the module Loading System is to solve the dependencies between different javascript libraries, rather than helping you develop a javascript library.

So what is a javascript module loading system?

The module system mainly aims to solve the naming conflicts between operating objects in different javascript libraries and the dependencies between different javascript libraries. The module loading system is for large-scale web Front-end applications or giant web front-end applications.

Generally, in a giant web front-end application page, the page has rich functions and complex services. As time passes, the page functions often change, as a result, front-end developers often need to develop functional modules for new functions. However, in the actual business, the functions of each functional module may also penetrate into each other and depend on each other. The relationship is complex, when pages are complex, the relationship between the front-end databases becomes difficult to manage and control. In this case, the module loading system will come in handy.

For most programmers, there are not many opportunities to independently undertake such a large web front-end application, but there are many opportunities to develop small and medium-sized web Front-end applications, such as enterprise-level web projects, there are few types of javascript libraries used in such projects, and the dependencies between libraries are well controlled. It is not necessary to introduce any module management system. Even if many small and medium Internet companies have webpages, it is estimated that it is not as complicated as the front-end of Enterprise web applications, so the relationship between modules or javascript libraries is well managed. In fact, these small and medium-sized applications are aimed at some or a specific scenario. Therefore, I personally think that we can finally form an independent javascript library for such a web front-end project, the features of this library should be similar to those of jQuery: the mode of adding several plug-in libraries to a master database is designed to solve the problem of universality, it can be reused and migrated, and the purpose of the plug-in library is often related to the Business Code. However, to distinguish the scope of the main library and plug-in library, so I added the namespace feature to the library.

The Javascript module loading technology shares some similarities with hadoop technology, that is, they are all aimed at super large systems, and they can play their role only under certain conditions, these technologies are all introduced from large Internet companies, because the problems that large Internet companies must solve when their applications become more complex. When your system is still in its infancy, these technologies are often used with caution. We should find the simplest and most effective way to solve our actual problems. If you think this system will become larger and larger in the future, you should keep the interfaces that will use these technologies later. If you use them too early, it is very likely that when the system scale is expanded, the cost of code refactoring will be higher.

For a module loading system, the most suitable scenario is to solve the decoupling problem between large web front-end application modules. If we need to write a new javascript file, we will immediately use the module loading technology, this is not a suspicion of misuse of technology. Before using a technology, we should not only consider how it is used, how it is used, but also whether it has any value.

Finally, I want to talk about it. I think that small and medium-sized web Front-end applications are deployed in production. Because javascript is not the most complex, it is best to pack all external javascript files into a javascript external file, the advantage is that the number of http requests is reduced. Using the module loading technology will make it very difficult to package files and even fail to do so (for example, the modules of requirejs and seajs are all file-based, each module is an independent file), which is contrary to the purpose of reducing http.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.