把很多复杂组件通过一个简单易使用的接口暴露出来
class Buffer extends Array {
constructor(width=30, height=20){
super();
this.width = width;
this.height = height;
this.alloc(width*height)
}
write(text, position=0){
}
}
class Viewport {
constructor(buffer=new Buffer()){
this.buffer = buffer;
this.offset = 0;
}
append(text, pos){
this.buffer.write(text, pos + this.offset);
}
getCharAt(index){
return this.buffer[this.offset + index];
}
}
class Console{
constructor(){
this.buffer = new Buffer();
this.currentViewport = new Viewprot(this.buffer);
this.buffer = [this.buffer];
this.viewports = [this.currentViewport];
}
write(text){
this.currentViewport.buffer.write(text);
}
getCharAt(index){
return this.currentViewport.getChartAt(index);
}
}
let c = new Console();
c.write('hello');
let ch = c.getCharAt(0);