You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
// Сделать функцию пользователя которая берет за основу userInfo и за счет замыкания создает новый объект, с которым можно работать как user1().increase(100)
2
+
3
+
constuserInfo={
4
+
balance: 0,
5
+
operations: 0,
6
+
increase(sum){
7
+
this.balance+=sum;
8
+
this.operations++;
9
+
},
10
+
};
11
+
12
+
functioncreateUser(template){
13
+
constnewUser={ ...template};
14
+
return()=>newUser;
15
+
}
16
+
17
+
constuser1=createUser(userInfo);// в итоге здесь функция, готовая вернуть/создать объект
18
+
user1().increase(100);// создание объекта и сразу отработка метода
0 commit comments