javascript - 编写一个带有方法的类

标签 javascript class methods

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

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

'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');
  });
});

我编写了以下代码,但我不确定我做错了什么或从这里开始哪里。有人可以帮忙告诉我测试发生了什么以及我应该做什么吗? :

 class Editor {
  constructor (str) {
    this.str = str;
  }

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

  toString(){
  
  }
}

最佳答案

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;
  }
}

您需要保留编辑器字符串的历史记录...

关于javascript - 编写一个带有方法的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44619335/

相关文章:

javascript - 无法使用 ng-Bootstrap 日期选择器

javascript - 创建 react 组件以有条件地包装子组件

javascript - ionic2 - 仅当用户在 Toast 中单击 "close"时才创建函数

python - python中继承类的默认值

java - 无法解析或不是字段(处理中)

Javascript:如何从方法内的函数获取值

javascript - 使用 Slick jQuery slider 的问题

javascript - 有没有办法在 Javascript 中说 collection[attr1=value].attr2=value ?

java - 方法应该返回 boolean,返回 int

javascript - .join 方法是否将数组更改为 Javascript 中的字符串?