//单利
// class Single {
// constructor(name) {
// this.name = name;
// }
// static getInstance(name) {
// // 静态方法
// if (!this.instance) {
// // 关键代码 this指向的是Single这个构造函数
// this.instance = new Single(name);
// }
// return this.instance;
// }
// }
// let single1 = Single.getInstance("name1");
// let single2 = Single.getInstance("name2");
// console.log(single1.name,single2.name); // true
class Combine {
constructor() {
this.list = [];
}
add(fn) {
console.log('add',fn)
this.list.push(fn);
return this; // 链式调用
}
excute() {
for (let i = 0; i < this.list.length; i++) {
console.log(this.list[i])
if(this.list[i].excute1){
this.list[i].excute1();
}else{
this.list[i].excute()
}
}
}
}
let comb1 = new Combine();
comb1
.add({
excute1() {
console.log(1);
}
})
.add({
excute1() {
console.log(2);
}
});
let comb2 = new Combine();
comb2
.add({
excute1() {
console.log(3);
}
})
.add({
excute1() {
console.log(4);
}
});
let comb3 = new Combine();
comb3
.add({
excute1() {
console.log(5);
}
})
.add({
excute1() {
console.log(6);
}
});
comb2.add(comb3);
let comb4 = new Combine();
comb4.add(comb2).add(comb1);
comb4.excute();
console