组合模式
小于 1 分钟约 140 字
const Folder = function (folder) {
this.folder = folder
this.list = []
}
Folder.prototype.add = function (res) {
this.list.push(res)
}
Folder.prototype.scan = function () {
console.log('扫描文件夹', this.folder)
for (let i = 0; i < this.list.length; i++) {
this.list[i].scan()
}
}
const File = function (file) {
this.file = file
}
File.prototype.scan = function () {
console.log('开始扫描文件', this.file)
}
let rootFolder = new Folder('root')
let htmlFolder = new Folder('html')
let cssFolder = new Folder('css')
let jsFolder = new Folder('js')
let html4 = new File('html4')
let html5 = new File('html5')
let css2 = new File('css2')
let css3 = new File('css3')
let es5 = new File('es5')
let es6 = new File('es6')
rootFolder.add(htmlFolder)
rootFolder.add(cssFolder)
rootFolder.add(jsFolder)
htmlFolder.add(html4)
htmlFolder.add(html5)
cssFolder.add(css2)
cssFolder.add(css3)
jsFolder.add(es5)
jsFolder.add(es6)
rootFolder.scan()