模块化开发

一.为什么要进行模块化开发(传统开发的弊端)

  传统开发中不同js的引入可能出现这样或那样的问题,像那些变量命名冲突了,文件依赖加载顺序问题等等,尤其是到团队合作的时候,这些问题就更显得尤为重要

  传统开发缺点:耦合度太高,代码关联性太强,不便于后期维护,会造成全局污染

  1.命名冲突问题(示例)

  module1.js

var module=function(){
    cosonle.log('I am module1.js');
}

  module2.js

var module=function(){
    console.log("I am module2.js");
}

  test.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="js/module1.js"></script>
    <script src="js/module2.js"></script>
</head>
<body>

</body>
</html>
<script>
    var module=function(){
        console.log('I am module3');
    };
    module();
</script>

当运行test.html时结果输出:

原文地址:https://www.cnblogs.com/Shinigami/p/10970210.html