zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Javascript] Module Pattern

JavaScript module pattern
2023-09-14 09:00:45 时间

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.

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.