Skip to content

Commit 8bfff85

Browse files
committed
[EX-16.7/st-compl] creating-new-user-obj
Creating a "new" obj/user by "closure". Worth noting: - that there is no need to complicate things, an MVP solution may be enough. FS-dev: B-3 / JS basic
1 parent 39e4f1b commit 8bfff85

File tree

1 file changed

+21
-0
lines changed
  • full-stack-dev/3-js-basic/16-managing-this/16-7-ex-creating-new-user-obj

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Сделать функцию пользователя которая берет за основу userInfo и за счет замыкания создает новый объект, с которым можно работать как user1().increase(100)
2+
3+
const userInfo = {
4+
balance: 0,
5+
operations: 0,
6+
increase(sum) {
7+
this.balance += sum;
8+
this.operations++;
9+
},
10+
};
11+
12+
function createUser(template) {
13+
const newUser = { ...template };
14+
return () => newUser;
15+
}
16+
17+
const user1 = createUser(userInfo); // в итоге здесь функция, готовая вернуть/создать объект
18+
user1().increase(100); // создание объекта и сразу отработка метода
19+
20+
console.log(user1()); // { balance: 100, operations: 1, increase: ƒ }
21+
console.log(userInfo); // { balance: 0, operations: 0, increase: [Function: increase] }

0 commit comments

Comments
 (0)