Skip to content

Commit 9a8f185

Browse files
committed
[EX-16.4/st-compl] reset-user-password
Reset obj/user "password", using "bind()" or "call()" meth. Worth noting: - preferring the "call()" method for this case. FS-dev: B-3 / JS basic
1 parent 515e9d8 commit 9a8f185

File tree

1 file changed

+48
-0
lines changed
  • full-stack-dev/3-js-basic/16-managing-this/16-4-ex-reset-user-password

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Нужно согласно объекта пользователя, и сторонней функции.. организовать возможность сброса пароля, посредствам отработки логики bind() или call() метода:
2+
// function removePassword(reset) {
3+
// if (reset) {
4+
// this.password = undefined;
5+
// } else {
6+
// this.password = 1;
7+
// }
8+
// }
9+
//
10+
// const user = {
11+
// login: '[email protected]',
12+
// password: '12345',
13+
// };
14+
// Функция принимает один аргумент reset, который определяет, стоит ли сбросить пароль пользователя (true - сбросить).
15+
16+
'use strict';
17+
18+
function removePassword(reset) {
19+
if (reset) {
20+
this.password = undefined;
21+
} else {
22+
this.password = 1;
23+
}
24+
}
25+
26+
const user = {
27+
28+
password: '12345',
29+
30+
resetPassword(confirm) {
31+
removePassword.call(user, confirm);
32+
},
33+
};
34+
35+
user.resetPassword(false);
36+
console.log(user.password); // 1
37+
38+
user.resetPassword(true);
39+
console.log(user.password); // undefined
40+
41+
// ?? альтернативное решение (через bind())
42+
const newResetPassword = removePassword.bind(user);
43+
44+
newResetPassword(true);
45+
console.log(user.password); // undefined
46+
47+
newResetPassword(false);
48+
console.log(user.password); // 1

0 commit comments

Comments
 (0)