filmov
tv
JavaScript Modules In 90 Seconds #JavaScriptJanuary
![preview_player](https://i.ytimg.com/vi/4hmzMpBE4gE/maxresdefault.jpg)
Показать описание
Watch the entire #JavaScriptJanuary series from the beginning!
Modules allow you to break up your code into separate files. This helps to organize and keep your code clean.
A module can export code in several ways, and this will affect the way the consumer imports it.
One way to export something is by setting a default. So we can add “export default” to the beginning of the item we are exporting.
We also have a consumer which is the main JavaScript file that is defined in the HTML file. Here we can import the function like this. Note that this requires either a relative or absolute URL, so use either ./ or / before the file name, whichever is applicable.
Now we can use the function in our consumer.
When importing a default export, you can name the import whatever you want.
Note that you can only have one default export per module and a default export is not required.
We can export more that one thing from a module. We just need to add export before it.
Now in our consumer we can import those as well. Note that the default export does not require curly braces but the others do.
We can also rename these in our consumer by using an alias.
So what is the benefit of using modules? Modules are tree shakable (import what you need, shake off the rest) The consumer can import only what they need, which results in smaller bundles and better performance.
ES Modules was introduced in 2015, however as with any newer technology, it is important to check compatibility. As of the date of this video, browser compatibility for JavaScript modules is at 88%.
So if you need to support older browsers you will want to use something like babel to compile your code.
I'll be releasing a new video every day in January! Stay tuned..
_____________________________________
📚 Learn to CODE in just a FEW months here:
_____________________________________
🛠️ Tools I use:
_____________________________________
💖 Show support!
_____________________________________
Watch Next:
_____________________________________
Connect With Me:
_____________________________________
** Affiliate Disclaimer: Some of the above links may be affiliate links, which may generate me a sales commission at no additional cost to you.
#JavaScriptJanuary #31Days31Videos #codeSTACKr
Modules allow you to break up your code into separate files. This helps to organize and keep your code clean.
A module can export code in several ways, and this will affect the way the consumer imports it.
One way to export something is by setting a default. So we can add “export default” to the beginning of the item we are exporting.
We also have a consumer which is the main JavaScript file that is defined in the HTML file. Here we can import the function like this. Note that this requires either a relative or absolute URL, so use either ./ or / before the file name, whichever is applicable.
Now we can use the function in our consumer.
When importing a default export, you can name the import whatever you want.
Note that you can only have one default export per module and a default export is not required.
We can export more that one thing from a module. We just need to add export before it.
Now in our consumer we can import those as well. Note that the default export does not require curly braces but the others do.
We can also rename these in our consumer by using an alias.
So what is the benefit of using modules? Modules are tree shakable (import what you need, shake off the rest) The consumer can import only what they need, which results in smaller bundles and better performance.
ES Modules was introduced in 2015, however as with any newer technology, it is important to check compatibility. As of the date of this video, browser compatibility for JavaScript modules is at 88%.
So if you need to support older browsers you will want to use something like babel to compile your code.
I'll be releasing a new video every day in January! Stay tuned..
_____________________________________
📚 Learn to CODE in just a FEW months here:
_____________________________________
🛠️ Tools I use:
_____________________________________
💖 Show support!
_____________________________________
Watch Next:
_____________________________________
Connect With Me:
_____________________________________
** Affiliate Disclaimer: Some of the above links may be affiliate links, which may generate me a sales commission at no additional cost to you.
#JavaScriptJanuary #31Days31Videos #codeSTACKr
Комментарии