有沒有這樣的經歷,在編寫代碼的時候,因為功能簡單,寫的時候比較隨意,所有的JS代碼都放在一個文件裡面,但是隨著功能的增加,發現代碼很亂,不好維護。
簡單的整理了一下,目前對已有項目的結構做了一個分析,主要有以下兩種方式
css/img/js/app.jscontrollers.jsdirectives.jsfilters.jsservices.jslib/partials/index.html
存在問題: 如果有很多 controller.js 或者 services.js 文件,則 JS 文件夾裡面會很亂,當然可以繼續在 JS 文件夾裡面,創建 controllers 文件夾, services 文件夾
繼續創建文件夾進行歸納,則已經有了大概的結構了。
但是 又來了一個問題,如果模塊多了,為了區分 這些文件屬於哪一個模塊,就比較麻煩。
按模塊進行劃分,可以劃分的細一點。
優勢: 方便模塊的添加和刪除,刪除模塊只需要在 modules 文件夾,刪除掉 指定的模塊,然後再 routes 文件裡面,刪除相對應的路由,就可以了
commonimgsjscssfonts...moduleshomecssjs //文件夾裡面可以在根據需要,創建新的文件夾imgsindex.js //入口文件index.html
<html><body><input type="text" name="username" id="username" value="" /><input type="password" name="password" id="password" value="" /><script type="text/javascript">if(document.getElementById("username").value == ""){alert("用戶名不能為空");}if(document.getElementById("password").value == ""){alert("密碼不能為空");}</script></body></html>
function check_username() {if (document.getElementById("username").value == "") {alert("用戶名不能為空");}}function check_password() {if (document.getElementById("password").value == "") {alert("密碼不能為空");}}
1.使用類的方法
function register() {this.check_username = function () {if (document.getElementById("username").value == "") {alert("用戶名不能為空");}}this.check_password = function () {if (document.getElementById("password").value == "") {alert("用戶名不能為空");}}}new register().check_username(); //調用方法
2.使用對象的方法
var register = {check_username: function () {if (document.getElementById("username").value == "") {alert("用戶名不能為空");}},check_password: function () {if (document.getElementById("password").value == "") {alert("用戶名不能為空");}}}register.check_username(); //調用方法
[top]
參考文章: