Skip to content

Commit b3332de

Browse files
committed
Completed memento
1 parent 2140763 commit b3332de

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,14 +1408,12 @@ First of all we have our memento object that will be able to hold the editor sta
14081408

14091409
```js
14101410
class EditorMemento {
1411-
protected content
1412-
1413-
constructor(string content) {
1414-
this.content = content
1411+
constructor(content) {
1412+
this._content = content
14151413
}
14161414

14171415
getContent() {
1418-
return this.content
1416+
return this._content
14191417
}
14201418
}
14211419
```
@@ -1424,37 +1422,39 @@ Then we have our editor i.e. originator that is going to use memento object
14241422

14251423
```js
14261424
class Editor {
1427-
protected content = ''
1425+
constructor(){
1426+
this._content = ''
1427+
}
14281428

1429-
type(string words) {
1430-
this.content = this.content . ' ' . words
1429+
type(words) {
1430+
this._content = this._content + ' ' + words
14311431
}
14321432

14331433
getContent() {
1434-
return this.content
1434+
return this._content
14351435
}
14361436

14371437
save() {
1438-
return new EditorMemento(this.content)
1438+
return new EditorMemento(this._content)
14391439
}
14401440

1441-
restore(EditorMemento memento) {
1442-
this.content = memento.getContent()
1441+
restore(memento) {
1442+
this._content = memento.getContent()
14431443
}
14441444
}
14451445
```
14461446

14471447
And then it can be used as
14481448

14491449
```js
1450-
editor = new Editor()
1450+
const editor = new Editor()
14511451

14521452
// Type some stuff
14531453
editor.type('This is the first sentence.')
14541454
editor.type('This is second.')
14551455

14561456
// Save the state to restore to : This is the first sentence. This is second.
1457-
saved = editor.save()
1457+
const saved = editor.save()
14581458

14591459
// Type some more
14601460
editor.type('And this is third.')
@@ -1465,7 +1465,7 @@ console.log(editor.getContent())// This is the first sentence. This is second. A
14651465
// Restoring to last saved state
14661466
editor.restore(saved)
14671467

1468-
editor.getContent() // This is the first sentence. This is second.
1468+
console.log(editor.getContent()) // This is the first sentence. This is second.
14691469
```
14701470

14711471
😎 Observer

0 commit comments

Comments
 (0)