Module pattern provide a way to have both public and private pieces with the export keyword. This protects values from leaking into the global scope or ending up in a naming collision.
![[Javascript] Module Pattern](https://blog.ytso.com/wp-content/themes/justnews/themer/assets/images/lazy.png)
secretis a private variable inside Math.js which is not accessible outside the file.
How to use module partten in Javascript env?
HTML:
If doing like this:
<script src="math.js"></script>
All the math.js variable will be global.
Doing this:
<script src="index.js" type="module" defer></script>
Apply modular pattern.
Node.js
If you want to use “import & export”, you have to add "type": "module",to package.json.
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/webdev/282336.html