It is necessary to understand "w is it integrated?" about each.windowHomeglobalHome https://developer.mozilla.org/ja/docs/Glossary/Global_object Comment In the historical background, the global object is named by the implementation of JavaScript.windowNode.jsglobalContact Us This was a confusion, so the latest ECMAScript (a language based on JavaScript) specificationsglobalThiscan be used as a global object. (See the table below for each name.) )windowGlobalglobalOld browser (Internet Explorer, Chrome71,Edge79 less,Firefox65 less,Safari12.1 less) ○× × ×× × ×Old Node.js (under 12)× × × ○× × ×New browser (Chrome71 or higher, Edge79 or higher, Firefox65 or higher, Safari12.1 or higher) ○× × × ○New Node.js (12+)× × × ○ ○* In the old version of babel and core-js, even in the browser environmentglobalbecause polyfill is modified, even if it is a code that works in the browser,globalI used it. Interneting Internet Explorer (IE), this version is currently supported.globalThisThere is no modern browser or Node.js that cannot be used. If you don't use old version or IE,windowHomeglobalinstead ofglobalThisYou don’t have to worry about anything.* New version of babel and core-jsglobalThiscontains polyfill.In JavaScirpt, global object properties become global variables (in other words, global variables are global objects properties). ). In other words, it is said that it is a global variable that can be used anywhere by assigning it as a property of a global object. For example, like before$HomejQueryIf you want to use it anywhere, you can write as follows:import jQuery from 'jquery';
globalThis.jQuery = jQuery;
globalThis.$ = jQuery;
This method is not limited to webpack and can be used with JavaScirpt as a module. In most cases, it seems that there is no problem here. PreviousgloablThisinstead ofwindow(premises running on browser) orglobal(Node.js, or if you are using a browser, you only need to use polyfill). P in ProvideP。in also reads the library as a global variable, and the result is almost the same. However, this is useful when you say "I don't want to write a library on the source code" or "I want to use it as a whole without the description of the source code".As a way of writing old JavaScript without webpack, JavaScript was not loaded with another script tag, and then the JavaScript was only written on the premise that JavaScript was loaded. I think there is a possibility that I want to write such as now. In such a case, if you load the using the rrP in function, you will not need to load the individually. Especially for polyfills such as core-js, if you want to remove IE-compatible or if you want to see the version of the browser that corresponds to the future, there is the advantage that JavaScript code can be handled by changing webpack settings without changing.It's a long time ago<stript src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
Instead of writing it, when I tried to embed the 。 on the chord side that I couldn’t use CDN, the original chord changed by 。rP。in.TheinP、in problem is that the library is written in another place than theJavaScript code of JavaScript. When you try to change the library to read, you may not know where to read.In summary, if it is polyfill, I think thatrrP in is better, but it is better to avoid the general library, except when it is assumed that the library exists globally. application.jsglobalThisIt is a simple story to say that it is easier to follow the code. Which name is used for global objects?globalThisNo problem. If you want to support IE, you can include core-js and other polyfills.Although it is a bit serpent, it is a problem that global variables are contaminated because both methods are added to global variables. If the global variable crashes its name, the solution is very difficult. Therefore, in modern writing, it is better not to use global variables as much as necessary. For example, you can use jQUery only for JavaScript files.import jQuery from 'jquery';
const $ = jQuery;
If you write it, you can use it only. This can be summarized in one when webpack joins, even if it is multiple files, so it will not say that the loading of the itself will be multiple times.So what about onclick, but if you want to write a code directly on HTML, you only need to make something global. In addition, I think that it is a better way to remove elements by class and ID, and use addEventListener etc. is to separate views and logic. On the other hand, if tags and scripts are very strong, I think that the shape that dynamically generatesOM using React etc will become a better code. In any case, you should consider the possibility that there is a limitation somewhere in the way of using onclick on HTML.