当前位置:网站首页>JS 模块、闭包应用
JS 模块、闭包应用
2022-07-27 12:52:00 【Ares-Wang】

-------------------------------------------模------块----------------------------------------------------------------------------
-------单独的myModule1.js文件-----------
function myModule1(){
var msg=‘Ares_Wang’ // 私有变量
//操作数据的函数
function doSomething(){
console.log(‘doSomething()’+msg.toUpperCase())
}
function doSomething(){
console.log(‘doOthing()’+msg.toLowerCase())
}
//向外暴露对象(给外部使用的方法)
return {
doSomething:doSomething, // ES6 可以简写doSomething,
doOtherthing:doOtherthing // ES6 可以简写doOtherthing
}
}
---------------------html-界面----------------------------
<script type="text/javascript" src="myModule1.js"></script>
<script> var module=myModule1() //一个对象 module.doSomthing() module.doOtherthing() </script>
---------------------利用IIFE—实现模块-------------------------------------------------
-------单独的myModule2.js文件-----------
(function (window){
var msg=‘Ares_Wang’ // 私有变量
//操作数据的函数
function doSomething(){
console.log(‘doSomething()’+msg.toUpperCase())
}
function doSomething(){
console.log(‘doOthing()’+msg.toLowerCase())
}
//向外暴露对象(给外部使用的方法)
window.myModule2= {
doSomething:doSomething, // ES6 可以简写doSomething,
doOtherthing:doOtherthing // ES6 可以简写doOtherthing
}
})(window)---------------------html-界面----------------------------
<script type="text/javascript" src="myModule2.js"></script>
<script> myModule2.doSomthing() myModule2.doOtherthing() </script>
边栏推荐
- 初学者入门:使用WordPress搭建一个专属自己的博客
- Vertical and horizontal shooting range - the mystery of the picture
- Double material first!
- Write a program, accept a string consisting of letters, numbers and spaces, and a character, and then output the number of characters in the input string. Case insensitive.
- Can I only use tidb binlog tool to synchronize tidb to MySQL in real time?
- Seata 在蚂蚁国际银行业务的落地实践
- Unapp prevents continuous click errors
- eBPF/Ftrace
- 固定定位
- Feign's dynamic proxy
猜你喜欢

52:第五章:开发admin管理服务:5:开发【分页查询admin账号列表,接口】;(Swagger的@ApiParam(),对方法参数进行注释;PageHelper分页插件;拦截器拦截检查登录状态)

JNI程序如何进行参数传递

Evconnlistener of libevent_ new_ bind
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (IX)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (IX)

V-on basic instruction

字节跳动 AI Lab 总监李航:语言模型的过去、现在和未来

js基础知识整理之 —— 数组

leetcode——83,24;机器学习——神经网络

Build mtk6765 compilation environment

腾讯云联合中国工联院发布工业AI质检标准化研究成果加速制造业智能化转型
随机推荐
Verilog的系统任务----$fopen、$fclose和$fdisplay, $fwrite,$fstrobe,$fmonitor
工具及方法 - 在线流程图描画
【基础知识】~ 集成电路设计流程,以及各阶段所使用的EDA工具
滑环设备怎么进行维护
【C进阶】指针数组 VS 数组指针
网络异常流量分析系统设计
v-text
Write a program, accept a string consisting of letters, numbers and spaces, and a character, and then output the number of characters in the input string. Case insensitive.
What are the precautions for using carbon brushes
How to pass parameters in JNI program
Two call processors of feign
Rotation chart
C ftp add, delete, modify, query, create multi-level directory, automatic reconnection, switch directory
Go语言系列:如何搭建Go语言开发环境?
echart折线图默认显示最后一个点以及纵向虚线
shell环境变量以及set,env,export的区别
Realize the disk partition and file system mount of the newly added hard disk
腾讯云联合中国工联院发布工业AI质检标准化研究成果加速制造业智能化转型
Qt剪切板QClipboard 复制粘贴自定义数据
52:第五章:开发admin管理服务:5:开发【分页查询admin账号列表,接口】;(Swagger的@ApiParam(),对方法参数进行注释;PageHelper分页插件;拦截器拦截检查登录状态)