- Published on
๐ JS(9,10,11)-๋๋ฆผ์ฝ๋ฉ ๊ฐ์ ์ ๋ฆฌ
- ๊ธ์ด์ด
๐ ๋ชฉ์ฐจ
โจ ์๋ฐ์คํฌ๋ฆฝํธ ๊ธฐ์ด ๊ฐ์ (ES5+): ๊ฐ์ด ๋ ธํธ๋ฅผ ์์ฑํ๋ฉฐ ๋ฐฐ์์ ๐
๐๐ป
- ์๋ฐ์คํฌ๋ฆฝํธ์ ์ญ์ฌ์ ๋ชฉ์ ๋ฐ ๋ํฅ
- ์๋ฐ์คํฌ๋ฆฝํธ ๋์์๋ฆฌ
Array APIs ์ด์ ๋ฆฌ(9๊ฐ)
์ ์ฉํ 10๊ฐ์ง ๋ฐฐ์ด ํจ์
Q1. make a string out of an array
์ฃผ์ด์ง ๋ฐฐ์ด์ ๋ฌธ์์ด๋ก ๋ฌถ์ด์ ๋ฐํํ๊ธฐ -> join()
ํจ์ ์ฌ์ฉ
๊ตฌ๋ถ์๋ฅผ ()
์์ ๋ง์๋๋ก ์ธ ์ ์์
{
const fruits = ['apple', 'banana', 'orange'];
const result = fruits.join(',');
console.log(result);
}
Q2. make an array out of a string
split()
ํจ์
{
const fruits = '๐, ๐ฅ, ๐, ๐';
const result = fruits.split(',');
console.log(result);
}
Q3. make this array look like this: [5, 4, 3, 2, 1]
reverse()
ํจ์
{
const array = [1, 2, 3, 4, 5];
const result = array.reverse();
console.log(result);
console.log(array);
}
Q4. make new array without the first two elements
array.slice()
ํจ์
{
const array = [1, 2, 3, 4, 5];
const result = array.slice(2, 5);
console.log(result);
console.log(array);
}
class Student {
constructor(name, age, enrolled, score) {
this.name = name;
this.age = age;
this.enrolled = enrolled;
this.score = score;
}
}
const students = [
new Student('A', 29, true, 45),
new Student('B', 28, false, 80),
new Student('C', 30, true, 90),
new Student('D', 40, false, 66),
new Student('E', 18, true, 88),
];
Q5. find a student with the score 90
find()
ํจ์
{
const result = students.find((student) => student.score === 90);
console.log(result);
}
Q6. make an array of enrolled students
filter()
ํจ์
{
const result = students.filter((student) => student.enrolled);
console.log(result);
}
Q7. make an array containing only the students' scores
map()
ํจ์
// result should be: [45, 80, 90, 66, 88]
{
const result = students.map((student) => student.score);
console.log(result);
}
Q8. check if there is a student with the score lower than 50
some()
ํจ์
{
console.clear();
const result = students.some((student) => student.score < 50);
console.log(result);
const result2 = !students.every((student) => student.score >= 50);
console.log(result2);
}
console.clear();
Q9. compute students' average score
reduce()
ํจ์
{
const result = students.reduce((prev, curr) => prev.score + curr.score)
console.log(result / students.length)
}
Q10. make a string containing all the scores
์ฌ๋ฌ๊ฐ์ ํจ์๋ฅผ method chaining์ผ๋ก ๋ฌถ์ด ์ฌ์ฉ
{
const result = students
.map((student) => student.score)
.filter((score) => score >= 50)
.join();
console.log(result);
}
// Bonus! do Q10 sorted in ascending order
// result should be: '45, 66, 80, 88, 90'
{
const result = students
.map((student) => student.score)
.sort((a, b) => b - a)
.join();
console.log(result);
}
JSON (10๊ฐ)
HTTP
: Hypertext Transfer Protocol
AJAX
์ ์ข
๋ฅ ์ค ํ๋๋ก XHR์ด ์์
XML
์ html
๊ณผ ๊ฐ์ด ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ํ์ผ ํฌ๋งท
XHR
: XMLHttpRequest
์์ฆ์ XML
๋์ JSON
์ ๋ง์ด ์ฌ์ฉํ๋ค.
JSON
์
- simplest data interchange format
- lightweight text-based structure
- easy to read
- key-value pairs
- used for serialization and transmission of data between the network the network connection
- independent programming language and platform
1.Object to JSON
// stringfy(obj)
let json = JSON.stringify(true)
console.log(json)
json = JSON.stringify(['apple', 'banana'])
console.log(json)
const rabbit = {
name: 'tori',
color: 'white',
size: null,
birthDate: new Date(),
jump: function () {
console.log(`${this.name} can jump!`)
},
}
json = JSON.stringify(rabbit)
console.log(json)
json = JSON.stringify(rabbit, ['name', 'color', 'size'])
console.log(json)
json = JSON.stringify(rabbit, (key, value) => {
console.log(`key: ${key}, value: ${value}`)
return key === 'name' ? 'ellie' : value
})
console.log(json)
2. JSON to Object
// parse(json)
console.clear()
json = JSON.stringify(rabbit)
console.log(json)
const obj = JSON.parse(json, (key, value) => {
console.log(`key: ${key}, value: ${value}`)
return key === 'birthDate' ? new Date(value) : value
})
console.log(obj)
rabbit.jump()
// obj.jump();
console.log(rabbit.birthDate.getDate())
console.log(obj.birthDate.getDate())
Callback (11๊ฐ)
๋น๋๊ธฐ ์ฒ๋ฆฌ์ ์์ ์ฝ๋ฐฑ~~~
1. Callback? ๋๊ธฐ์ ๋น๋๊ธฐ
์๋ฐ์คํฌ๋ฆฝํธ๋ Synchronous ์ธ์ด์
๋๋ค!
์ด๋ง์ธ ์ฆ์จ hoisting์ด ๊ฐ๋ฅํ๋ค๋ ๊ฒ์
๋๋ค.
์ฝ๋ฐฑ ํจ์๋ ๋น๋๊ธฐ์ ์ผ๋ก ๋ด๊ฐ ์ํ ๋,์ํ๋ ๊ณณ์์ ๋ถ๋ฅผ ์ ์๋ค.(setTimeout๋ฑ์ผ๋ก)
2. Synchronous callback (์ฆ๊ฐ์ ์ผ๋ก ๋ฐ์ํ๋ ์ฝ๋ฐฑ ํจ์)
function printImmediately(print) {
print()
}
printImmediately(() => console.log('hello'))
3. Asynchronous callback (์๊ฐ์ ์ ํด๋๊ณ ๊ทธ ๋ค์ ์คํํ๋ ์ฝ๋ฐฑ ํจ์)
function printWithDelay(print, timeout) {
setTimeout(print, timeout)
}
printWithDelay(() => console.log('async callback'), 2000)
4. Callback Hell example
// Callback Hell example
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)
}
}
const userStorage = new UserStorage()
const id = prompt('enter your id')
const password = prompt('enter your passrod')
userStorage.loginUser(
id,
password,
(user) => {
userStorage.getRoles(
user,
(userWithRole) => {
alert(`Hello ${userWithRole.name}, you have a ${userWithRole.role} role`)
},
(error) => {
console.log(error)
}
)
},
(error) => {
console.log(error)
}
)