lil.dev
Published on

๐ŸŽ‰ JS(9,10,11)-๋“œ๋ฆผ์ฝ”๋”ฉ ๊ฐ•์˜ ์ •๋ฆฌ

๊ธ€์“ด์ด

    ๐Ÿ“Œ ๋ชฉ์ฐจ

    Welcome

    โœจ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ ๊ฐ•์˜ (ES5+): ๊ฐ™์ด ๋…ธํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋ฉฐ ๋ฐฐ์›Œ์š” ๐Ÿ“’

    ๐Ÿ’๐Ÿป

    1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์—ญ์‚ฌ์™€ ๋ชฉ์  ๋ฐ ๋™ํ–ฅ
    2. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋™์ž‘์›๋ฆฌ

    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)
      }
    )