// 以文本编辑器为例,该编辑器会不时地保存状态,并且可以根据需要进行恢复。classEditorMemento{// memento对象将能够保持编辑器状态constructor(content){this._content = content;}getContent(){returnthis._content;}}classEditor{constructor(){this._content ="";}type(words){this._content =`${this._content}${words}`;}getContent(){returnthis._content;}save(){returnnewEditorMemento(this._content);}restore(memento){this._content = memento.getContent();}}(function(){const editor =newEditor() editor.type("This is the first sentence."); editor.type("This is second.");const saved = editor.save(); editor.type("And this is third.") console.log(editor.getContent());// This is the first sentence. This is second. And this is third. editor.restore(saved); console.log(editor.getContent());// This is the first sentence. This is second.})();