Skip to content

Commit aed16d0

Browse files
committed
Extract commit as a separate function
1 parent 6de4f73 commit aed16d0

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

JavaScript/1-proxy.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
'use strict';
22

33
const start = (data) => {
4-
console.log('start transaction');
4+
console.log('\nstart transaction');
55
let delta = {};
6+
const commit = () => {
7+
console.log('\ncommit transaction');
8+
Object.assign(data, delta);
9+
delta = {};
10+
};
611
return new Proxy(data, {
712
get(target, key) {
8-
if (key === 'commit') {
9-
return () => {
10-
console.log('commit transaction');
11-
Object.assign(data, delta);
12-
delta = {};
13-
};
14-
}
13+
if (key === 'commit') return commit;
1514
if (delta.hasOwnProperty(key)) return delta[key];
1615
return target[key];
1716
},
@@ -26,7 +25,7 @@ const start = (data) => {
2625

2726
// Usage
2827

29-
const data = { name: 'Marcus Aurelius', city: 'Rome', born: 121 };
28+
const data = { name: 'Marcus Aurelius', born: 121 };
3029

3130
console.log('data.name', data.name);
3231
console.log('data.born', data.born);

0 commit comments

Comments
 (0)