把很多复杂组件通过一个简单易使用的接口暴露出来

    1. class Buffer extends Array {
    2. constructor(width=30, height=20){
    3. super();
    4. this.width = width;
    5. this.height = height;
    6. this.alloc(width*height)
    7. }
    8. write(text, position=0){
    9. }
    10. }
    11. class Viewport {
    12. constructor(buffer=new Buffer()){
    13. this.buffer = buffer;
    14. this.offset = 0;
    15. }
    16. append(text, pos){
    17. this.buffer.write(text, pos + this.offset);
    18. }
    19. getCharAt(index){
    20. return this.buffer[this.offset + index];
    21. }
    22. }
    1. class Console{
    2. constructor(){
    3. this.buffer = new Buffer();
    4. this.currentViewport = new Viewprot(this.buffer);
    5. this.buffer = [this.buffer];
    6. this.viewports = [this.currentViewport];
    7. }
    8. write(text){
    9. this.currentViewport.buffer.write(text);
    10. }
    11. getCharAt(index){
    12. return this.currentViewport.getChartAt(index);
    13. }
    14. }
    15. let c = new Console();
    16. c.write('hello');
    17. let ch = c.getCharAt(0);