Why is javascript no better way to include files?

I saw several ways in which you can create a javascript file, including other javascript files, but they all look pretty hacky - basically they include binding the javascript file to the end of the current document, and then downloading it somehow.

Why doesn't javascript include the simple “load this file and execute the script in it” include directive? This does not seem to be a new concept. I know that everyone is thrilled that everyone does HTML5 using javascript, etc., but wouldn’t it be hard if you have to hack the lack of basic functions like this?

I don’t see how this would be a security issue, since a web page can include as many javascript files as they like, and they all execute anyway.

+4
source share
3 answers

The main problems with the current inclusion system (i.e. adding additional script tags) are related to the delay. Since the script tag can insert code at the inclusion point as soon as the script tag is encountered, further parsing should stop longer or less until the JS is loaded and executed (although the browser can continue fetching resources in parallel). If JS decides to start the inclusion, you just added more latency on top of it - now you can’t even use your scripts in parallel.

Basically, he is trying to solve a problem that does not exist (since JS can already reference additional script tags to make inclusion), making the delay problem even worse. There are javascript minifiers that can combine JS files; you should study them instead, as they will also help improve latency issues.

+3
source

Actually, YUI 3 perfectly solves this problem. Feel free to check out the documentation: http://developer.yahoo.com/yui/3/yui/#use (this is a special use function that does this magic). It basically works as follows:

  • You define modules
  • When you create the main YUI object using YUI (), you indicate which modules need your code.
  • Behind the scenes, the YUI checks to see if these modules are loaded. If not, it asynchronously loads them onto the page.

I also read that the jQuery team is working on something similar (someone here supports me).

Regarding the philosophical argument that it would be nice if it were built in, I think it could be a good feature. On the other hand, the simplicity of javascript is also good. This allows a much lower entry point for novice programmers to do their job. And for those of us who need it, large libraries like YUI are getting better every day.

+2
source

the requirejs project is trying to solve this problem, see for example

http://requirejs.org/docs/why.html

(I do not use it yet)

0
source

Source: https://habr.com/ru/post/1336835/


All Articles