[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.

[Javascript] Module Pattern

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/282336.html

(0)
上一篇 2022年8月26日
下一篇 2022年8月26日

相关推荐

发表回复

登录后才能评论