>[success] # 案例扫面文件夹 ~~~ 1.在整个常见业务中最像树结构的'文件夹','部门'书中以文件夹的形式举了个案例 ~~~ >[danger] ##### 案例代码 ~~~ // 文件夹 var Folder = function (name) { this.name = name this.files = [] } Folder.prototype.add = function (file) { this.files.push(file) } // 扫描 所有文件 Folder.prototype.scan = function () { console.log('开始扫面文件夹:'+this.name) for(var i=0,file,files=this.files;file = files[i++];){ file.scan() } } // 文件相当于叶子 var File = function (name) { this.name = name } File.prototype.add = function () { throw new Error('文件下面不能添加内容') } File.prototype.scan = function () { console.log('开始扫描文件:'+this.name) } // 使用 // 根目录 var folder = new Folder( '学习资料' ); // 二级目录 var folder1 = new Folder( 'JavaScript' ); var folder2 = new Folder ( 'jQuery' ); // 文件 var file1 = new File( 'JavaScript 设计模式与开发实践' ); var file2 = new File( '精通 jQuery' ); var file3 = new File( '重构与模式' ) folder1.add( file1 ); folder2.add( file2 ); folder.add( folder1 ); folder.add( folder2 ); folder.add( file3 ); // 代用执行 folder.scan(); ~~~