[Javascript] Module Pattern

2022/8/27 1:23:21

本文主要是介绍[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.

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.

  



这篇关于[Javascript] Module Pattern的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程