[Javascript] Module Pattern
作者:互联网
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.
secret
is 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
.
标签:use,Javascript,Pattern,global,private,js,export,Module,pattern 来源: https://www.cnblogs.com/Answer1215/p/16627678.html