首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用方法编写类

使用方法编写类
EN

Stack Overflow用户
提问于 2017-06-19 04:38:20
回答 1查看 52关注 0票数 0

我正在尝试编写一个包含一些方法的类。我不确定我做错了什么。

为了创建这个类,我需要查看以下测试:

代码语言:javascript
复制
'use strict';

const Editor = require('../editor');
const { expect } = require('chai');

describe('Editor', () => {
  it('allows users to write text', () => {
    const editor = new Editor();

    editor.write('Hello - codez');
    expect(editor.toString()).to.equal('Hello - codez');

    editor.write('moar');
    expect(editor.toString()).to.equal('Hello - codez moar');
  });

  xit('allows users to undo writes', () => {
    const editor = new Editor();

    editor.write('Hello - codez');
    expect(editor.toString()).to.equal('Hello - codez');

    editor.write('Moar stuff');
    expect(editor.toString()).to.equal('Hello - codezMoar stuff');

    editor.write('Even more');
    expect(editor.toString()).to.equal('Hello - codezMoar stuffEven more');

    editor.undo();
    expect(editor.toString()).to.equal('Hello - codezMoar stuff');

    editor.undo();
    expect(editor.toString()).to.equal('Hello - codez');

    editor.undo();
    expect(editor.toString()).to.equal('');
  });

  xit('allows users to find and replace', () => {
    const editor = new Editor();

    editor.write('foo stuff');
    editor.write(' other foo');
    editor.replace('foo', 'bar');
    expect(editor.toString()).to.equal('bar stuff other bar');
  });

  xit('allows undo replaces', () => {
    const editor = new Editor();

    editor.write('foo stuff');
    editor.write(' other foo');
    editor.replace('foo', 'bar');
    expect(editor.toString()).to.equal('bar stuff other bar');

    editor.undo();
    expect(editor.toString()).to.equal('foo stuff other foo');

    editor.undo();
    expect(editor.toString()).to.equal('foo stuff');
  });

  xit('allows users to redo', () => {
    const editor = new Editor();

    editor.write('foo stuff');
    editor.write(' other foo');
    editor.replace('foo', 'bar');
    expect(editor.toString()).to.equal('bar stuff other bar');

    editor.undo();
    expect(editor.toString()).to.equal('foo stuff other foo');

    editor.undo();
    expect(editor.toString()).to.equal('foo stuff');

    editor.redo();
    expect(editor.toString()).to.equal('foo stuff other foo');

    editor.redo();
    expect(editor.toString()).to.equal('bar stuff other bar');
  });
});

我写了以下代码,但我不确定我做错了什么,也不知道从哪里开始。谁能帮帮忙,告诉我考试是怎么回事,我该怎么做?:

代码语言:javascript
复制
 class Editor {
  constructor (str) {
    this.str = str;
  }

  write(text) {
      let newSentence = text + this.str;
     console.log('This is the this str', newSentence);
  }

  toString(){
  
  }
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-19 04:52:01

代码语言:javascript
复制
class Editor {
  constructor (str) {
    this.str = str;
    this.states = [""]; //for undo
    this.undos = [];
  }
  write(text) {
    this.undos = [];
    this.states.push(this.str);
    this.str += text;
  }
  undo(){
    this.undos.push(this.str);
    this.str = this.states.pop() || "";
  }
  redo(){
    if(this.undos.length){
      this.states.push(this.str);
      this.str = this.undos.pop();
    }
  }
  replace(a,b){
    this.states.push(this.str);
    this.str = this.str.split(a).join(b);
  }
  toString(){
    return this.str;
  }
}

你需要保存编辑器字符串的历史记录...

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44619335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档