Some thoughts on the technology of JavaScript module loading

Source: Internet
Author: User

Shortly before a netizen asked me to use Requirejs and Seajs at the front end, I asked him if your company had previously written JavaScript libraries, or JavaScript frameworks, and his answer was nothing. He just heard that like Requirejs and Seajs is a new technology, very valuable so want to use it.

The netizen's question caused me to think about the JavaScript module loading technology, the last article I gave the basic structure of a JavaScript library that I wrote myself, In fact, one of the reasons for writing this article is because I want to use a technique like requirejs or seajs to redesign the basic model of my JavaScript library, and when I delve into this technology, I find it incorrect to use the module loading system to solve the problem of decoupling common code and business code from JavaScript libraries, and the module loading system is scoped to solve the dependency problem between different JavaScript libraries, rather than helping you develop a JavaScript library.

So what is JavaScript's module loading system?

module system is mainly to solve the problem of naming conflicts among different JavaScript libraries and the dependencies between different JavaScript libraries, the module loading system is for large web front-end applications or giant web front-end applications.

The general huge web front-end application page, the function of the page is very rich, the business is very complex, and over time, the function of the page will often change, so that the front-end developers often have to develop a new function for the function module, but the actual business function between the function modules may also penetrate each other, Interdependent, complex relationship, when the page complexity, the relationship between the front-end libraries is difficult to manage and control problems, this time the module loading system will come in handy.

For most programmers, the chances of being able to take on such a large web front end application are not too much, and there are many opportunities for developing small and medium web front-end applications, such as Enterprise-class Web projects, where there are few JavaScript libraries to use, and the dependencies of each library are well controlled. It is not necessary to introduce what module management system, even if many small and medium-sized Internet company's Web page is not as complex as the enterprise Web application front-end, so its modules or the relationship between JavaScript library is well managed. In fact, like these small and medium-sized applications are for some or a specific scenario, so I personally feel that facing such a web front-end project, we can finally form a separate JavaScript library, This library should be characterized by the same type of jquery: A main library with several plug-in libraries, the main library is designed to solve the problem of commonality, it should be reusable and migrated, and the purpose of the plug-in library is often related to business code, but in order to distinguish between the main library and the plug-in library of the scope problem, So I added a namespace function to the library.

JavaScript module loading technology and Hadoop technology are some of the same point, that is, they are for the technology of large-scale systems, they can only play their role under certain conditions, so these technologies are from large Internet companies to launch out, Because large-scale internet companies have to solve the problem with the application of large and complex, when you are still at the beginning of the system, the use of these techniques are often cautious, we should find the simplest and most effective way to solve our actual problems, if you think this system will become more and more large, Then you should keep the interfaces for using these technologies later, and if you use them too early, it is likely that you will have a higher cost of refactoring your code when the system is scaled up.

For the module loading system, it is the most suitable scenario is to solve the problem of decoupling between large web front-end application modules, if we just write a new JavaScript file immediately using the module loading technology, this is not a bit of misuse of technology suspicion, we use a technology should not just consider how it is used, How to use, should also think about using it has no value to the problem.

Finally, I would like to say that I think the small and medium web front end is applied to production deployment, because JavaScript is not the most complex, so all external JavaScript files packaged into a JavaScript external file is the best, the advantage is to reduce the number of HTTP requests, Using the module loading technology will make it cumbersome to package files, even if they are not possible (modules like Requirejs and Seajs are file-based, each module is a standalone file), which contradicts the goal of reducing HTTP.

Some thoughts on the technology of JavaScript module loading

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.