Javascript模块化的写法—原始写法
- 模块就是实现特定功能的一组方法。
只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块。
比如:tool.js
function aaa(){}function bbb(){}
这种做法的缺点很明显:”污染”了全局变量,无法保证不与其他模块发生变量名冲突,而且模块成员之间看不出直接关系。
Javascript模块化的写法—对象写法
var moudleA = {var count = 0;m1:function (){}m2:function (){}}moudleA.m1()moudle.m2()
这样写的话会暴露模块中的所有成员,内部状态可以被外部状态改写。
Javascript模块化的写法—立即执行函数(闭包)
var moduleA = (function(){var count = 0; //私有变量function showA(){ //私有方法count += 10;console.log(count);}function showB(){count *= 5;console.log(count);}return {outA: showA,outB: showB}})();moduleA.outA();moduleA.outB();console.log(showA);
解决了全局变量的污染,使其私有化,闭包后的程序无法进行二次编写
Javascript模块化的写法—放大模式
如果一个模块很大,必须分成几个部分,或者一个模块需要继承另一个模块,这时就有必要采用”放大模式”(augmentation)。
var moduleA = (function () {var count = 0; //私有变量function showA() {//私有方法count += 10;console.log(count);}function showB() {count *= 5;console.log(count);}return {outA: showA,outB: showB,};})();moduleA = (function(mod){function showC(){console.log("我是showC");}mod.outC = showC;return mod;})(moduleA);moduleA.outA();moduleA.outB();moduleA.outC();
Javascript模块化的写法—宽放大模式
在浏览器环境中,模块的各个部分通常都是从网上获取的,有时无法知道哪个部分会先加载。如果采用上一节的写法,第一个执行的部分有可能加载一个不存在空对象,这时就要采用”宽放大模式”。 ```javascript
var moduleA = (function (mod) {var count = 0; //私有变量function showA() {//私有方法count += 10;console.log(count);}function showB() {count *= 5;console.log(count);}mod.outA = showA;mod.outB = showB;return mod;})(moduleA || {});
var moduleA = (function(mod){function showC(){console.log("我是showC");}mod.outC = showC;return mod;})(moduleA || {});moduleA.outA();moduleA.outB();moduleA.outC();
<a name="YSAEC"></a>## require.js- 主要功能是:将页面上所有的.js文件,进行模块化。【注】将这个页面要引入的js文件,全部变成模块。- 声明异步加载从这个文件开始,所有的后缀都省略。<br /> 【注】一个.html独享一个入口文件。这个入口文件负责管理当前页面上所有的js文件。```javascript<script src = 'js/require.js' defer async = 'true' data-main = 'js/main'></script>
主js页面的写法
- 对于引入模块的路径,我们可以提前引入
- 第一个参数是一个数组,写着要引入的模块的路径,第二个参数是回调函数,在回调函数的形参位置,我们可以引入对应的模块 ```javascript console.log(“加载成功”); //配置路径 require.config({ paths: { add: “demo/add” } }) require([“add”], function(add){ console.log(add.addOut(10, 20)); add.showOut(); })
<a name="r5ziE"></a>### 副js写法```javascriptdefine(function(){function add(x, y){return x + y;}function show(){console.log("hello world");}//对外暴露return {addOut: add,showOut: show}})define(function(){function drag(node){node.onmousedown = function(ev){var e = ev || window.event;var offsetX = e.clientX - this.offsetLeft;var offsetY = e.clientY - this.offsetTop;document.onmousemove = function(ev){var e = ev || window.event;var l = e.clientX - offsetX;l = range(l, 0, document.documentElement.clientWidth - node.offsetWidth);var t = e.clientY - offsetY;t = range(t, 0, document.documentElement.clientHeight - node.offsetHeight);node.style.left = l + 'px';node.style.top = t + 'px';}}document.onmouseup = function(){document.onmousemove = null;}}//限制出界function range(iCur, iMin, iMax){if(iCur <= iMin){return iMin;}else if(iCur >= iMax){return iMax;}else{return iCur;}}//限制出界函数return {drag: drag,range: range// drag}})
如果副js依赖另一个副js的写法
define(['drag'], function(drag){/*node1 是要缩放的节点node2 是拖拽的节点当前模块,依赖另外一个模块*/function scale(node1, node2){node2.onmousedown = function(ev){var e = ev || window.event;var l = e.clientX;var t = e.clientY;var w = node1.offsetWidth;var h = node1.offsetHeight;document.onmousemove = function(ev){var e = ev || window.event;//计算两次鼠标的偏移位置var W = drag.range(w + e.clientX - l, 100, 500);var H = drag.range(h + e.clientY - t, 100, 500);node1.style.width = W + 'px';node1.style.height = H + 'px';}}document.onmouseup = function(){document.onmousemove = null;}}return {scale: scale}})
jquery-cookie 依赖于jquery和某一模块不遵从AMD的写法
require.config({paths: {jquery: "jquery-1.11.3","jquery-cookie": "jquery.cookie",parabola: "parabola",index: "index"},//jquery-cookie 依赖于jqueryshim: {//设置依赖关系"jquery-cookie": ["jquery"],//某一个模块,不遵从AMDparabola: {exports: "_",},},})
define(["jquery", "jquery-cookie"], function($){//jquery 和 jquery-cookie 可以通过形参 $拿到//编写一些代码在页面上//点击页面弹出点击function main(){$(document).click(function(){alert("点击");})}function cookieFunc(){var aBtns = $("button");aBtns.eq(0).click(function(){$.cookie("变种人", "万磁王", {expires: 7})$.cookie("超级赛亚人", "孙悟空", {expires: 30})$.cookie("葫芦娃", "金刚葫芦娃", {expires: 1000})})aBtns.eq(1).click(function(){console.log($.cookie("变种人"));console.log($.cookie("超级赛亚人"));console.log($.cookie("葫芦娃"));})aBtns.eq(2).click(function(){$.cookie("葫芦娃", null);})}return {main: main,cookieFunc: cookieFunc}})
