오늘도 맵에 관해서 사투를 하고 있다.
https://www.youtube.com/watch?v=QOVV71EbgBA
mdn
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Tree.prototype.map = function(callback) {
let tree = new Tree();
tree.value = callback(this.value);
if (this.children) {
for (let el of this.children) {
tree.children.push(el.map(callback));
}
}
return tree;
};
근데 callback이 너무 어렵다
https://www.youtube.com/watch?v=j0Viy3v97gY
'use strict';
//Synchronous callback
function printImmediately(print) {
print()
}
printImmediately(() => console.log('hello'))
/// Asynchronous callback
function printWithDelay(print,timeout) {
setTimeout(print, timeout);
}
console.log('1');
setTimeout( () =>console.log('2'),1000 )
console.log('3');
printImmediately(() => console.log('hello'));
printWithDelay(()=> console.log('async callback'),2000)
1 at callback.js:19:0
3 at callback.js:21:0
hello
2
async callback
class UserStorage {
loginUser(id,password,onSuccess, onError){
setTimeout(() => {
if(
(id ==='ellie' && password === 'dream') ||
(id ==='coder' && password === 'academy')
) {
onSuccess(id);
} else {
onError(new Error('not found'))
}
}, 2000)
}
getRoles(user, onSuccess, onError) {
setTimeout(() => {
if( user ===' ellie') {
onSuccess({name:'ellie', role: 'admin'});
} else {
onError(new Error('no access'));
}
},1000)
}
}
/// 1. id,password
/// 2. login
/// 3
const userStorage = new UserStorage();
const id = prompt('enter your id');
const password = prompt('enter your password');
userStroage.loginUser(
id,
password,
user => {
userStorage.getRoles(
user,
userWithRole => {
alert(`Hello ${useWithRole.name}, you have a ${userWithRole.role} role`)
},
error => {
console.log(error);
}
);
},
error => {
console.log(error)
}
);
이건 뭔소리냐