js架构

本文介绍了六种不同的JavaScript模块设计及加载方式,包括直接定义、立即执行函数、变量封装、Sea.js异步加载等,展示了如何通过不同方法实现模块化的代码组织。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.本身和引用了才能调用

var CommentMenthod;
if(!CommentMenthod){
	CommentMenthod={};
}
CommentMenthod={
	changeIPMode:function(iType) {
		alert(iType);
	}
}
$(function(){
	CommentMenthod.changeIPMode(12345);
})

2.只有本身可以调用

(function () {
	function init() {
	}
	function changeIPMode(iType) {
	   alert("切换事件");
	}
	$(init());
})()

3.需要JS开放才能调用

var lliulol = (function () {
    function changeIPMode(iType) {
        alert("切换事件");
    }
    return {
        changeIPMode: changeIPMode
    }
} ())

4.本身和引用了才能调用(采用seajs加载)

//模块封装 ParkBlockCreate.js
define(function (require, exports, module) {
	ParkBlockCreate = {
		InitPopup: function (currOrgData) {},
    }
	module.exports = ParkBlockCreate;
})

//调用
require.async(["ParkBlockCreate.js"], function (BrandManage) {
   BrandManage.InitPopup();
});

5.跟随页面(采用seajs加载)

//html
<script>
    seajs.use("/MonitorCenter/VideoCenter/VidoPlayBack", function (main) {
        main.initial();
    });
</script>

//js
define(function (require, exports, module) {
    var VideoPlayBack = (function () {
         function initial() {
         }
        return {
            initial: initial
        }
    } ())
    module.exports = VideoPlayBack;
})

6.开放才能调用(采用seajs加载)

//引用
var MonitorDevice = require("/MonitorCenter/MonitorDevice.js");

//定义模块
define(function (require, exports, module) {
    var MonitorDevice = (function () {
           function ShowRefreshDevice(){
        }
        return {
            ShowRefreshDevice: ShowRefreshDevice
        }
    } ())
    module.exports = MonitorDevice;
})
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值