var Folder = function(name){
this.name = name;
this.parent = null;
this.files = [];
}
Folder.prototype.add = function(file){
file.parent = this;
this.files.push(file)
}
Folder.prototype.scan = function(){
console.log('begin scan')
for(var i = 0,file,files = this.files;file = files[i++];){
file.scan()
}
}
Folder.prototype.remove = function(){
if(!this.parent){
return
}
for(var files = this.parent.files, l = files.length -1 ;l >=0;l--){
var file = files[l];
if(file === this){
files.splice(l,1)
}
}
}
var File = function(name){
this.name = name;
this.parent = null;
}
File.prototype.add = function(){
throw new Error('throw add')
}
File.prototype.scan = function(){
console.log('begin scan'+this.name)
}
File.prototype.remove = function(){
if(!this.parent){
return
}
for(var files = this.parent.files,l = files.length -1;l>=0;l--){
var file = files[l]
if(file === this){
files.splice(l,1)
}
}
}
var folder = new Folder("学习资料")
var folder1 = new Folder("JavaScript")
var file1 = new Folder('深入浅出Node')
folder1.add(new File("JavaScript设计模式与开发实践"))
folder.add(folder1)
folder.add(file1)
folder1.remove()
console.log(111)
folder.scan()
console