【问题标题】:Write a Class with Methods用方法编写一个类
【发布时间】:2017-06-18 20:38:20
【问题描述】:

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

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

'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(){
  
  }
}

【问题讨论】:

  • this.str+=text; ... toString(){ return this.str;}

标签: javascript class methods


【解决方案1】:
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;
  }
}

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

【讨论】:

  • 感谢乔纳斯的来信。你能解释一下你是如何创建状态和撤消数组的吗?你的解决方案很棒。我想确保我理解它在做什么。
  • @ivonne terrero 要撤消更改,您要么跟踪它们(相当复杂),要么只存储所有状态。我们存储的最后一个状态将在撤消时恢复,因此它的先进后出 =>我们需要一个堆栈(push/pop)。同样的原则也适用于重做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
  • 1970-01-01
  • 2017-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多