ES6+ ๋ฌธ๋ฒ• (EX6+ Syntax)

JOY๐ŸŒฑยท2023๋…„ 4์›” 17์ผ
0

๐ŸŒย JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
12/12
post-thumbnail

๐Ÿ’โ€โ™€๏ธ ES6+ ๋ฌธ๋ฒ•์ด๋ž€,
ES6+๋Š” ECMAScript 6(2015)์˜ ํ™•์žฅ ๋ฒ„์ „์œผ๋กœ, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜, ํด๋ž˜์Šค, ๋ธ”๋ก ์Šค์ฝ”ํ”„ ๋ณ€์ˆ˜ ์„ ์–ธ, ๋””ํดํŠธ ๋งค๊ฐœ๋ณ€์ˆ˜, ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด, ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น, ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜, ์ „๊ฐœ ์—ฐ์‚ฐ์ž ๋“ฑ์ด ์žˆ์Œ

๐Ÿ‘€ ํด๋ž˜์Šค (Class)

๐Ÿ’โ€โ™€๏ธ ๋™์ผํ•œ ์ข…๋ฅ˜์˜ ๊ฐ์ฒด๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ์„ฑํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ์„ฑํ•˜๊ธฐ ๋ณด๋‹ค ํด๋ž˜์Šค ๋ฌธ๋ฒ•์„ ํ†ตํ•ด ๋™์ผํ•œ ์ข…๋ฅ˜์˜ ๊ฐ์ฒด๋ฅผ ์žฌ์ƒ์„ฑ ํ•  ์ˆ˜ ์žˆ์Œ


๐Ÿ™‹โ€ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์™€ ํด๋ž˜์Šค ๋ฌธ๋ฒ•์˜ ์ฐจ์ด

  • ํด๋ž˜์Šค ์ƒ์„ฑ์ž๋ฅผ new์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ์—๋Ÿฌ ๋ฐœ์ƒ
Teacher("์กฐ์ด");
// Student("์น˜์ฆˆ"); // TypeError: Class constructor Student cannot be invoked without 'new' ์˜ค๋ฅ˜
  • ํด๋ž˜์Šค์— ์ •์˜๋œ ๋ฉ”์†Œ๋“œ๋Š” ์—ด๊ฑฐ ๋ถˆ๊ฐ€ (for..in ์ˆœํšŒ ์‹œ ์ œ์™ธ)
  • ํด๋ž˜์Šค๋Š” ํ˜ธ์ด์ŠคํŒ…์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋™์ž‘
  • ํด๋ž˜์Šค๋Š” ํ•ญ์ƒ use strict ์ ์šฉ

๐Ÿ‘‰ ํด๋ž˜์Šค ์„ ์–ธ ๋ฐ ์‚ฌ์šฉ

class Student {

    // ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ๋ฐ ์ดˆ๊ธฐํ™”
    // ์ƒ์„ฑ์ž๋Š” 1๊ฐœ ์ด์ƒ ์ •์˜ ๋  ์ˆ˜ ์—†์œผ๋ฉฐ ์ƒ๋žตํ•  ๊ฒฝ์šฐ ์•”๋ฌต์ ์œผ๋กœ ์ •์˜ ๋จ
    constructor(name){
        this.group = 1;     // way 1. ๊ณ ์ • ๊ฐ’์œผ๋กœ ์ธ์Šคํ„ด์Šค ์ดˆ๊ธฐํ™”
        this.name = name;   // way 2. ์ „๋‹ฌ ๋ฐ›์€ ๊ฐ’์œผ๋กœ ์ธ์Šคํ„ด์Šค ์ดˆ๊ธฐํ™”
    }

    // ๋ฉ”์†Œ๋“œ ์ถ”๊ฐ€ (Student ํด๋ž˜์Šค์˜ ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์†Œ๋“œ)
    introduce() {
        console.log(`์•ˆ๋…•ํ•˜์„ธ์š” ์ €๋Š” ${this.group}๋ฐ˜ ๋ฉˆ๋จธ ${this.name}์ด์—์šฉ๐Ÿถ`);
    }

    // ์•”๋ฌต์ ์œผ๋กœ this๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ๋ฐ˜ํ™˜๋ฌธ ์ž‘์„ฑ X
}

let student1 = new Student("ํ—ˆ์น˜์น˜");    // ์ดˆ๊ธฐ๊ฐ’ ์ „๋‹ฌํ•˜๋ฉฐ ์ƒ์„ฑ
let student2 = new Student("ํ—ˆ๊ผฌ์ˆœ");    // ์ดˆ๊ธฐ๊ฐ’ ์ „๋‹ฌํ•˜๋ฉฐ ์ƒ์„ฑ
student1.introduce();                   // ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
student2.introduce();                   // ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
/* ํด๋ž˜์Šค ๋ฌธ๋ฒ•๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ๊ธฐ๋Šฅํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋Š” '์ƒ์„ฑ์ž ํ•จ์ˆ˜'๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ */
function Teacher(name) {
    this.group = 1;
    this.name = name;
}

Teacher.prototype.introduce = function() {
    console.log(`์•ˆ๋…•ํ•˜์‹ฌ๊นŒ ์ €๋Š” ${this.group}๋ฐ˜ ๋‹ด๋‹น ${this.name}์ž„๋‹ค๐Ÿ˜ณ`);
}

let teacher = new Teacher("ํšจ์—ฐ");
teacher.introduce();

๐Ÿ‘‰ ํด๋ž˜์Šค ํ‘œํ˜„์‹

๐Ÿ’โ€โ™€๏ธ ํด๋ž˜์Šค ํ‘œํ˜„์‹ ์ •์˜๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์˜ ์˜๋ฏธ๋Š” ํด๋ž˜์Šค๋„ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์ผ๊ธ‰ ๊ฐ์ฒด์ด๋ฉฐ ๋‹ค๋ฅธ ํ‘œํ˜„์‹ ๋‚ด๋ถ€์—์„œ ์ •์˜, ์ „๋‹ฌ, ๋ฐ˜ํ™˜, ํ• ๋‹น์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ

// 1. ์ต๋ช… ํด๋ž˜์Šค ํ‘œํ˜„์‹
let Tutor = class {
    teach() {
        console.log('์ดํ•ดํ–ˆ๋‚˜์šฉ๐Ÿ˜…');
    }
};

new Tutor().teach();
// 2. ๊ธฐ๋ช… ํด๋ž˜์Šค ํ‘œํ˜„์‹
let Tutee = class MyTutee {
    learn() {
        console.log("์Œ?..๋„ค๐Ÿ˜“");
        console.log(MyTutee);   // [class MyTutee] ์ถœ๋ ฅ => ํ•ด๋‹น ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    }
};

new Tutee().learn();
// console.log(MyTutee);   // ์ •์˜ X => ์™ธ๋ถ€์—์„œ ๋ถˆ๋Ÿฌ์„œ ์‚ฌ์šฉ ๋ถˆ๊ฐ€ํ•˜๋ฏ€๋กœ ํด๋ž˜์Šค ์ด๋ฆ„์˜ ์˜๋ฏธ X
/* 3. ํด๋ž˜์Šค ๋™์  ์ƒ์„ฑ */
function makeTutee(message) {

    /* ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด ์•„๋ž˜์˜ ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•˜๊ณ  ์ด๋ฅผ ๋ฐ˜ํ™˜ */
    return class{
        feedback() {
            console.log(message);
        }
    };
}

let secondTutee = makeTutee("10์  ๋งŒ์ ์— 10์ ๐Ÿ˜Ž");
new secondTutee().feedback(); // secondTutee์— ํด๋ž˜์Šค๊ฐ€ ๋‹ด๊ฒจ์žˆ๊ณ  ๊ทธ ํด๋ž˜์Šค ์•ˆ์˜ feedback() ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ

๐Ÿ‘‰ getter & setter

class Product {

    constructor(name, price) {
        /* 1. this๋“ค์€ setter ํ˜ธ์ถœ */
        this.name = name;
        this.price = price;

    }

    /* name, price๋ผ๋Š” ๊ฐ’์œผ๋กœ ์™ธ๋ถ€ ์ ‘๊ทผํ•˜๊ณ  (getter, setter ํ˜ธ์ถœ)
       getter, setter ๋‚ด๋ถ€์—์„œ๋Š” _name, _price๋กœ ์‹ค์ œ ๊ฐ’์— ์ ‘๊ทผ */

    // getter ํ•จ์ˆ˜
    get name() {
        console.log("get name ๋™์ž‘!");
        return this._name; // ์–ธ๋”๋ฐ” ์ž‘์„ฑ ํ•„์š” (์ž‘์„ฑํ•˜์ง€์•Š์œผ๋ฉด getter๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ๋จ -> ์ž๊ธฐ ์ž์‹  ํ˜ธ์ถœ -> Stack over flow Error)
    }
    
    get price() {
        console.log("get price ๋™์ž‘!");
        return this._price;
    }
    // setter ํ•จ์ˆ˜
    set name(value) {
        console.log("set name ๋™์ž‘!");
        this._name = value; // ์–ธ๋”๋ฐ” ์ž‘์„ฑ ํ•„์š” (์ž‘์„ฑํ•˜์ง€์•Š์œผ๋ฉด setter๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ๋จ -> ์ž๊ธฐ ์ž์‹  ํ˜ธ์ถœ -> Stack over flow Error)
    }
    
    set price(value) {
        console.log("set price ๋™์ž‘!");
        if(value <= 0) {
            console.log('๊ฐ€๊ฒฉ์€ ์Œ์ˆ˜์ผ ์ˆ˜ ์—†์–ด์œ ๐Ÿซฅ');
            this._price = 0;
            return;
        }
        this._price = value;
    }

}

let phone = new Product('์•„์ดํฐ 14 pro', 2000000);
console.log(phone.name, phone.price); /* 2. getter ํ˜ธ์ถœ */

let computer = new Product('๊ฐค๋Ÿญ์‹œ๋ถ3', -2500000);
console.log(computer.name, computer.price);

๐Ÿ‘‰ ์ƒ์†

// Animal ํด๋ž˜์Šค ์„ ์–ธ
class Animal {

    constructor(name, weight) {
        this.name = name;
        this.weight = weight;
    }

    eat(foodWeight) {
        this.weight += foodWeight;
        console.log(`${this.name}(์€)๋Š” ${foodWeight}kg๋งŒํผ์˜ ์‹์‚ฌ๋ฅผ ํ•˜๊ณ  ${this.weight}kg๊ฐ€ ๋˜์–ด๋ฒ„๋ ธ์–ด์š”๐Ÿ˜‚`);
    }

    move(lostWeight) {
        if(this.weight > lostWeight) this.weight -= lostWeight;
        console.log(`${this.name}(์€)๋Š” ์›€์ง์ž„์œผ๋กœ ์ธํ•ด ${lostWeight}kg๋งŒํผ ๊ฐ๋Ÿ‰๋˜์–ด ${this.weight}kg๊ฐ€ ๋˜์—ˆ๋„ค์šฉ!๐Ÿ˜ฏ`)
    }

}

// Animal์„ ์ƒ์†๋ฐ›๋Š” Human ํด๋ž˜์Šค ์„ ์–ธ - extends ํ‚ค์›Œ๋“œ ์‚ฌ์šฉ
class Human extends Animal {

    develop(language) {
        console.log(`${this.name}(์€)๋Š” ${language}๋กœ ๊ฐœ๋ฐœ์„ ํ•ด์š”~ ์Œ ์ž˜ํ•˜๋„ค์š”๐Ÿซ `);
    }

    dog(dogName) {
        console.log(`${dogName}๋Š” ${this.name}์˜ ๊ฐฑ์–ผ์ฅ์—์š”๐Ÿถ`);
    }
}

let human = new Human("์กฐ์ด", 50);

human.eat(2);
human.move(1);
human.move(3);
human.eat(5);

human.develop('JavaScript');
human.dog('์น˜์ฆˆ');

๐Ÿ’ป Mini Console

์กฐ์ด(์€)๋Š” 2kg๋งŒํผ์˜ ์‹์‚ฌ๋ฅผ ํ•˜๊ณ  52kg๊ฐ€ ๋˜์–ด๋ฒ„๋ ธ์–ด์š”๐Ÿ˜‚
์กฐ์ด(์€)๋Š” ์›€์ง์ž„์œผ๋กœ ์ธํ•ด 1kg๋งŒํผ ๊ฐ๋Ÿ‰๋˜์–ด 51kg๊ฐ€ ๋˜์—ˆ๋„ค์šฉ!๐Ÿ˜ฏ
์กฐ์ด(์€)๋Š” ์›€์ง์ž„์œผ๋กœ ์ธํ•ด 3kg๋งŒํผ ๊ฐ๋Ÿ‰๋˜์–ด 48kg๊ฐ€ ๋˜์—ˆ๋„ค์šฉ!๐Ÿ˜ฏ
์กฐ์ด(์€)๋Š” 5kg๋งŒํผ์˜ ์‹์‚ฌ๋ฅผ ํ•˜๊ณ  53kg๊ฐ€ ๋˜์–ด๋ฒ„๋ ธ์–ด์š”๐Ÿ˜‚
์กฐ์ด(์€)๋Š” JavaScript๋กœ ๊ฐœ๋ฐœ์„ ํ•ด์š”~ ์Œ ์ž˜ํ•˜๋„ค์š”๐Ÿซ 
์น˜์ฆˆ๋Š” ์กฐ์ด์˜ ๊ฐฑ์–ผ์ฅ์—์š”๐Ÿถ

๐Ÿ’โ€โ™€๏ธ ๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ
๋ถ€๋ชจ ๋ฉ”์†Œ๋“œ ์ „์ฒด๋ฅผ ๊ต์ฒดํ•˜์ง€ ์•Š๊ณ , ๋ถ€๋ชจ ๋ฉ”์†Œ๋“œ๋ฅผ ํ† ๋Œ€๋กœ ์ผ๋ถ€ ๊ธฐ๋Šฅ๋งŒ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์„ ๋•Œ, ๋ถ€๋ชจ ๋ฉ”์†Œ๋“œ์˜ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ๋ฉ”์†Œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ์„ ์‚ฌ์šฉ

class Animal {

    constructor(name, weight) {
        this.name = name;
        this.weight = weight;
    }

    eat(foodWeight) {
        this.weight += foodWeight;
        console.log(`${this.name}(์€)๋Š” ${foodWeight}kg๋งŒํผ์˜ ์‹์‚ฌ๋ฅผ ํ•˜๊ณ  ${this.weight}kg๊ฐ€ ๋˜์–ด๋ฒ„๋ ธ์–ด์š”๐Ÿ˜‚`);
    }

    move(lostWeight) {
        if(this.weight > lostWeight) this.weight -= lostWeight;
        console.log(`${this.name}(์€)๋Š” ์›€์ง์ž„์œผ๋กœ ์ธํ•ด ${lostWeight}kg๋งŒํผ ๊ฐ๋Ÿ‰๋˜์–ด ${this.weight}kg๊ฐ€ ๋˜์—ˆ๋„ค์šฉ!๐Ÿ˜ฏ`)
    }

}

// Animal์„ ์ƒ์† ๋ฐ›๋Š” Tiger ํด๋ž˜์Šค ์„ ์–ธ
class Tiger extends Animal {

    attack(target) {
        console.log(`${this.name}๐Ÿฏ(์ด)๊ฐ€ ${target}์„ ๊ณต๊ฒฉํ•ด์š”!๐Ÿซฃ`);
    }

    // Animal์˜ move๋ฅผ ํ™•์žฅํ•œ Tiger์˜ move
    move(target) {
        // super๋ฅผ ํ†ตํ•ด ๋ถ€๋ชจ ๊ฐ์ฒด ์ฐธ์กฐ ๊ฐ€๋Šฅ
        super.move(0.1);
        this.attack(target);
    } 

}

let tiger = new Tiger('์‹ ์‚ฌ๋™ ํ˜ธ๋žญ์ด', 90);
tiger.move('์ฐ์ฐ์ด๐Ÿญ');

๐Ÿ’ป Mini Console

์‹ ์‚ฌ๋™ ํ˜ธ๋žญ์ด(์€)๋Š” ์›€์ง์ž„์œผ๋กœ ์ธํ•ด 0.1kg๋งŒํผ ๊ฐ๋Ÿ‰๋˜์–ด 89.9kg๊ฐ€ ๋˜์—ˆ๋„ค์šฉ!๐Ÿ˜ฏ
์‹ ์‚ฌ๋™ ํ˜ธ๋žญ์ด๐Ÿฏ(์ด)๊ฐ€ ์ฐ์ฐ์ด๐Ÿญ์„ ๊ณต๊ฒฉํ•ด์š”!๐Ÿซฃ

๐Ÿ’โ€ ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋ผ์ด๋”ฉ

class Animal {

    constructor(name, weight) {
        this.name = name;
        this.weight = weight;
    }

    eat(foodWeight) {
        this.weight += foodWeight;
        console.log(`${this.name}(์€)๋Š” ${foodWeight}kg๋งŒํผ์˜ ์‹์‚ฌ๋ฅผ ํ•˜๊ณ  ${this.weight}kg๊ฐ€ ๋˜์–ด๋ฒ„๋ ธ์–ด์š”๐Ÿ˜‚`);
    }

    move(lostWeight) {
        if(this.weight > lostWeight) this.weight -= lostWeight;
        console.log(`${this.name}(์€)๋Š” ์›€์ง์ž„์œผ๋กœ ์ธํ•ด ${lostWeight}kg๋งŒํผ ๊ฐ๋Ÿ‰๋˜์–ด ${this.weight}kg๊ฐ€ ๋˜์—ˆ๋„ค์šฉ!๐Ÿ˜ฏ`)
    }

}

class Deer extends Animal {

    /* constructor ์žฌ์ •์˜ ๊ฐ€๋Šฅ */
    constructor(name, weight, legLength) {
        super(name, weight);
        this.legLength = legLength;
    }

    hide(place) {
        console.log(`${this.name}(์ด)๊ฐ€ ${place}์— ์ˆจ์—ˆ์–ด์š”!`);
    }
}

let deer = new Deer('์•„๊ธฐ ์‚ฌ์Šด ๋ฐค๋น„๐ŸฆŒ', 40, 1);
deer.eat(5);
deer.move(3);
deer.hide('ํ’€์ˆฒ ์‚ฌ์ด');

๐Ÿ‘€ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ (Arrow function)

๐Ÿ’โ€โ™€๏ธ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ž€?
ES6์—์„œ ๋„์ž…๋œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” function ํ‚ค์›Œ๋“œ ๋Œ€์‹  ํ™”์‚ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•ด ์ข€ ๋” ๊ฐ„๋žตํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Œ. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ํ•ญ์ƒ ์ต๋ช… ํ•จ์ˆ˜๋กœ ์ •์˜. ๋ณธ๋ฌธ์ด ํ•œ ์ค„์ธ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์œ ์šฉ

๐Ÿ‘‰ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ

let message;

// ๊ธฐ์กด function ์ •์˜
message = function () {
    return "Hello World!";
};

console.log(message());

// function ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ
message = () => {
    return "Arrow Function!";
};

console.log(message());

// ๋ช…๋ น๋ฌธ์ด ํ•˜๋‚˜๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ ์ค‘๊ด„ํ˜ธ ์ƒ๋žต ๊ฐ€๋Šฅ
// ์ด ๋•Œ ํ•จ์ˆ˜ ๋ชธ์ฒด ๋‚ด๋ถ€์˜ ๋ฌธ์ด ๊ฐ’์œผ๋กœ ํ‰๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ํ‘œํ˜„์‹์ธ ๋ฌธ์ด๋ผ๋ฉด ์•”๋ฌต์ ์œผ๋กœ ๋ฐ˜ํ™˜
// return ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ
message = () => "Arrow Functions are Simple!";

console.log(message());

// ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ
message = (val1, val2) => "Arrow " + val1 + val2;

console.log(message('Function', '!'));

// ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•˜๋‚˜๋ฉด ์†Œ๊ด„ํ˜ธ ์ƒ๋žต ๊ฐ€๋Šฅ
// ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๊ฑฐ๋‚˜ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ ์ƒ๋žต ๋ถˆ๊ฐ€
message = val => "Arrow " + val;

console.log(message('Functions are GOOD!!!'));

// ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ ์†Œ๊ด„ํ˜ธ๋กœ ๊ฐ์Œˆ
const createUser = (id, name) => ({ id, name });
console.log(createUser(1, '์œ ๊ด€์ˆœ'));
// ์†Œ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์ง€ ์•Š์„ ๊ฒฝ์šฐ ํ•จ์ˆ˜ ๋ชธ์ฒด ์ค‘๊ด„ํ˜ธ๋กœ ์ž˜๋ชป ํ•ด์„
const createUser2 = (id, name) => { id, name };
console.log(createUser2(2, 'ํ™๊ธธ๋™'));

// ๊ณ ์ฐจ ํ•จ์ˆ˜์— ์ธ์ˆ˜๋กœ ์ „๋‹ฌ ๊ฐ€๋Šฅํ•˜๋ฉฐ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜ ํ‘œํ˜„์‹ ๋ณด๋‹ค ํ‘œํ˜„์ด ๊ฐ„๊ฒฐํ•ด์ง
console.log([1,2,3,4,5].map(function(val){ return val * 10}));
console.log([1,2,3,4,5].map(val => val * 10));

๐Ÿ‘‰ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ ํŠน์ง•

  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋˜์–ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Œ
  • ์•„๋ž˜์™€ ๊ฐ™์€ ํŠน์ง•์€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๊ฐ€ ์™ธ๋ถ€ ํ•จ์ˆ˜์˜ this์™€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ์˜๋„์  ์„ค๊ณ„
// 1. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” this๋ฅผ ๊ฐ€์ง€์ง€ ์•Š์Œ
//    ์ด๋Ÿฌํ•œ ํŠน์ง•์€ ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ ์•ˆ์—์„œ ๋™์ผํ•œ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ˆœํšŒํ•˜๋Š”๋ฐ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
let theather = {
    store : '์šฉ์‚ฐ์•„์ดํŒŒํฌ์ ',
    titles : ['์–ด๋ฒค์ ธ์Šค', '๊ฒจ์šธ์™•๊ตญ', '์ŠคํŒŒ์ด๋”๋งจ', '๋ผ์ด์˜จํ‚น', '์•Œ๋ผ๋”˜', '๋ง˜๋งˆ๋ฏธ์•„', '๋ณด์Šค๋ฒ ์ด๋น„'],
    showMovieList() {
        /* ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ */
        // ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” this๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— this.store๋ฅผ ์‚ฌ์šฉ ์‹œ, theather ๊ฐ์ฒด ์•ˆ์˜ store์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ
        this.titles.forEach(title => console.log(this.store + " : " + title));

        /* ์ผ๋ฐ˜ ํ•จ์ˆ˜ */
        // ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด function์˜ this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๋‚˜ํƒ€๋ƒ„
        this.titles.forEach(function(title) {
            console.log(this); // undefined
            console.log(this.store + " : " + title);
        });
    }
};

theather.showMovieList();
// 2. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” new์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†์Œ
// this๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ
const arrowFunc = () => {};
// new arrowFunc(); // TypeError : arrowFunc is not a constructor
// ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ prototype ํ”„๋กœํผํ‹ฐ๊ฐ€ ์—†๊ณ  ํ”„๋กœํ† ํƒ€์ž…๋„ ์ƒ์„ฑํ•˜์ง€ ์•Š์Œ
console.log(arrowFunc.hasOwnProperty('prototype')); // false
// 3. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” super๋ฅผ ๊ฐ€์ง€์ง€ ์•Š์Œ
class Animal {

    constructor(name, weight) {
        this.name = name;
        this.weight = weight;
    }

    move(lostWeight) {
        if(this.weight > lostWeight) this.weight -= lostWeight;
        console.log(`${this.name}(์€)๋Š” ์›€์ง์ž„์œผ๋กœ ์ธํ•ด ${lostWeight}kg๋งŒํผ ๊ฐ๋Ÿ‰๋˜์–ด ${this.weight}kg๊ฐ€ ๋˜์—ˆ๋„ค์šฉ!๐Ÿ˜ฏ`)
    }

}

// Animal์„ ์ƒ์† ๋ฐ›๋Š” Tiger ํด๋ž˜์Šค ์„ ์–ธ
class Tiger extends Animal {

    move(lostWeight) {
        // ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” super๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์•„ super๋ฅผ ์™ธ๋ถ€ ํ•จ์ˆ˜์—์„œ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์—  Animal์˜ move๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋จ
        setTimeout(() => super.move(lostWeight), 3000);
        // setTimeout(function(){ super.move(lostWeight) }, 3000); // SyntaxError: 'super' keyword unexpected here
                                                                   // ์ผ๋ฐ˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์—๋Ÿฌ ๋ฐœ์ƒ
        
        console.log('์ฐ์ฐ์ด๐Ÿญ๋ฅผ ์ฐพ์•„ ์–ด์Šฌ๋ ๐Ÿฏ~');
    } 

}

let tiger = new Tiger('์ง€๋ฆฌ์‚ฐ ํ˜ธ๋žญ์ด', 90);
tiger.move(2);
// 4. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” arguments๋ฅผ ์ง€์› X
(function(){

    // ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ๋ณธ์ธ์˜ argument 3, 4๊ฐ€ ์•„๋‹Œ ์ƒ์œ„ ์Šค์ฝ”ํ”„์ธ ์ฆ‰์‹œ ์‹คํ–‰ ํ•จ์ˆ˜์˜ argument 1, 2๋ฅผ ์ฐธ์กฐ
    const arrowFunc = () => console.log(arguments); // [Arguments] { '0': 1, '1': 2 } ์ถœ๋ ฅ
    arrowFunc(3, 4);

})(1, 2);

๐Ÿ‘€ ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ• (Spread syntax)

๐Ÿ’โ€โ™€๏ธ ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•์ด๋ž€?
๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜(Rest parameter)์ฒ˜๋Ÿผ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ชฉ๋ก์„ ๋ฐฐ์—ด๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ๊ณผ๋Š” ๋ฐ˜๋Œ€๋กœ, ๋ฐฐ์—ด์„ ํ†ต์งธ๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ๊ธฐ๋Šฅ.
ํ•˜๋‚˜๋กœ ๋ญ‰์ณ์ ธ ์žˆ๋Š” ์—ฌ๋Ÿฌ ๊ฐ’๋“ค์˜ ์ง‘ํ•ฉ์„ ์ „๊ฐœํ•ด์„œ ๊ฐœ๋ณ„์ ์ธ ๊ฐ’๋“ค์˜ ๋ชฉ๋ก์œผ๋กœ ๋งŒ๋“ฌ

๐Ÿ‘‰ ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜ (Rest parameter)

function merge(msg1, msg2) {
    return msg1 + msg2;
}

console.log(merge('์•ˆ๋‡ฝ!'));                               // ์•ˆ๋‡ฝ!undefined (์ „๋‹ฌ๋œ ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†์–ด undefined)
console.log(merge('์•ˆ๋‡ฝ!', '๋ฐฉ๊ฐ€๋ฐฉ๊ฐ€~'));                   // ์•ˆ๋‡ฝ!๋ฐฉ๊ฐ€๋ฐฉ๊ฐ€~
console.log(merge('์•ˆ๋‡ฝ!', '๋ฐฉ๊ฐ€๋ฐฉ๊ฐ€~', '์ „ Joy์—์š”๐Ÿ™‹โ€โ™€๏ธ'));   // ์•ˆ๋‡ฝ!๋ฐฉ๊ฐ€๋ฐฉ๊ฐ€~ (๋งˆ์ง€๋ง‰ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋ฌด์‹œ๋จ)

// ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜ ...๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ•œ๋ฐ ๋ชจ์•„ ๋ฐฐ์—ด์— ๋‹ด์„ ์ˆ˜ ์žˆ์Œ
function mergeAll(...args) {

    let message = '';

    for(let arg of args) message += arg;

    return message;
}

console.log(mergeAll('์•ˆ๋‡ฝ!'));                                // ์•ˆ๋‡ฝ!
console.log(mergeAll('์•ˆ๋‡ฝ!', '๋ฐฉ๊ฐ€๋ฐฉ๊ฐ€~'));                    // ์•ˆ๋‡ฝ!๋ฐฉ๊ฐ€๋ฐฉ๊ฐ€
console.log(mergeAll('์•ˆ๋‡ฝ!', '๋ฐฉ๊ฐ€๋ฐฉ๊ฐ€~', '์ „ Joy์—์š”๐Ÿ™‹โ€โ™€๏ธ'));    // ์•ˆ๋‡ฝ!๋ฐฉ๊ฐ€๋ฐฉ๊ฐ€~์ „ Joy์—์š”๐Ÿ™‹โ€โ™€๏ธ


// ์œ ์˜ํ•  ์ ์€ ๋‚˜๋จธ์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•ญ์ƒ ๋งˆ์ง€๋ง‰์— ์žˆ์–ด์•ผํ•จ
// function func(arg1, ...args, arg2) {}    // ๋ถˆ๊ฐ€๋Šฅ
function func(arg1, arg2, ...args) {}       // ๊ฐ€๋Šฅ

๐Ÿ‘‰ ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ• (Spread syntax)

console.log(`๊ฐ€์žฅ ํฐ ๊ฐ’ ; ${ Math.max(10, 20, 30)}`);

/* 1. */
let arr = [10, 20, 30];
// Math.max๋Š” ๋ฐฐ์—ด์ด ์•„๋‹Œ ์ˆซ์ž ๋ชฉ๋ก์„ ์ธ์ˆ˜๋กœ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด์„ ์ „๋‹ฌํ•˜๋ฉด ์›ํ•˜๋Š” ๋Œ€๋กœ ๋™์ž‘ํ•˜์ง€ X
console.log(`๊ฐ€์žฅ ํฐ ๊ฐ’ : ${ Math.max(arr) }`);     // NaN
// ...(์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•) : ๋ฐฐ์—ด์ด ํ•ด๋‹น ๋ฐ์ดํ„ฐ ํƒ€์ž… ๊ทธ๋Œ€๋กœ ํŽผ์ณ์ง
console.log(`๊ฐ€์žฅ ํฐ ๊ฐ’ : ${ Math.max(...arr) }`);  // 30   

/* 2. */
let arr1 = [10, 30, 20];
let arr2 = [100, 300, 200];
// ๋‹ค๋ฅธ ๋ฐฐ์—ด ๊ฐ์ฒด ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ํ•œ๊บผ๋ฒˆ์— ํŽผ์ณ์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
console.log(`๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ : ${ Math.min(...arr1, ...arr2)}`);               // ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ : 10
// ์ผ๋ฐ˜ ๊ฐ’๊ณผ ํ˜ผํ•ฉํ•ด์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
console.log(`๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ : ${ Math.min(5, ...arr1, 15, ...arr2, 25)}`);    // ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’ : 5

/* 3. concat() ๋ฉ”์†Œ๋“œ๋ณด๋‹ค ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋ฐฐ์—ด ๋ณ‘ํ•ฉ์— ์‚ฌ์šฉ ๊ฐ€๋Šฅ */
let merged = [0, ...arr, 2, ...arr2];
console.log(merged);

/* 4. ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ */
let str = "JavaScript";
console.log([...str]);
// ['J', 'a', 'v', 'a','S', 'c', 'r', 'i','p', 't']

๐Ÿ‘‰ ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•์„ ์ด์šฉํ•œ ๋ฐฐ์—ด, ๊ฐ์ฒด ๋ณต์‚ฌ

/* 1. ๋ฐฐ์—ด ๋ณต์‚ฌ */
let arr = [10, 20, 30];
let arrCopy = [...arr];
console.log(arr);
console.log(arrCopy);
console.log(arr === arrCopy); // false(๋™์ผ ๊ฐ์ฒด๋Š” ์•„๋‹˜)

/* 2. ๊ฐ์ฒด ๋ณต์‚ฌ */
let obj = { name : 'ํ—ˆ์น˜์น˜', age : 20 };
let objCopy = { ...obj };
console.log(obj);
console.log(objCopy);
console.log(obj === objCopy); // false(๋™์ผ ๊ฐ์ฒด๋Š” ์•„๋‹˜)

๐Ÿ‘€ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น (Distructuring assignment)

๐Ÿ™‹โ€ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด๋ฅผ ๋ณ€์ˆ˜๋กœ ๋ถ„ํ•ดํ•˜์—ฌ ์—ฐ๊ฒฐ ๊ฐ€๋Šฅ

๐Ÿ‘‰ ๋ฐฐ์—ด ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

let nameArr = ["Cheese", "Heo"];

/* ์ธ๋ฑ์Šค ํ•˜๋‚˜ํ•˜๋‚˜ ์ ‘๊ทผํ–ˆ๋˜ ๋ฐฉ์‹ */
// let firstName = nameArr[0];
// let lastName = nameArr[1];
/* ๊ตฌ์กฐ ๋ถ„ํ•ด ๋ฌธ๋ฒ•์œผ๋กœ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌ */
let [firstName, lastName] = nameArr;
console.log(firstName);
console.log(lastName);

/* ๋ฐ˜ํ™˜ ๊ฐ’์ด ๋ฐฐ์—ด์ธ split ๋ฉ”์†Œ๋“œ๋ฅผ ํ™œ์šฉ */
let [firstName2, lastName2] = "Chichi Heo".split(" ");
console.log(firstName2);
console.log(lastName2);

/* ์‰ผํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•„์š”ํ•˜์ง€ ์•Š์€ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ๋ฒ„๋ฆด ์ˆ˜ ์žˆ์Œ */
let [firstName3, , lastName3] = ['Hyoyeon', 'Joy', 'Cho'];
console.log(firstName3);
console.log(lastName3);

๐Ÿ’โ€ ๋‹ค์–‘ํ•œ ์‚ฌ์šฉ๋ฒ•

/* 1. ๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ์— ์‚ฌ์šฉ */
let user = {};
[user.firstName, user.lastName] = "Zzanga Shin".split(" ");
console.log(user);
/* 2. ๋ณ€์ˆ˜ ๊ตํ™˜ ์šฉ๋„๋กœ ์‚ฌ์šฉ */
let student = "ํ•™์ƒ";
let teacher = "์Œค";

[student, teacher] = [teacher, student]; // [๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น ๋ฌธ๋ฒ•] = [๋ฐฐ์—ด] 

console.log(`student : ${student}, teacher : ${teacher}`); // student : ์Œค, teacher : ํ•™์ƒ
/* 3. rest parameter ...๋กœ ๋‚˜๋จธ์ง€ ์š”์†Œ๋ฅผ ํ•œ ๋ฒˆ์— ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ */
let [sign1, sign2, ...rest] = ['์–‘์ž๋ฆฌ', '๋ฌผ๋ณ‘์ž๋ฆฌ', 'ํ™ฉ์†Œ์ž๋ฆฌ', '๊ฒŒ์ž๋ฆฌ', '์‚ฌ์ž์ž๋ฆฌ']; // ...rest์—๋Š” ๋‚˜๋จธ์ง€ ์š”์†Œ๋“ค์˜ ๋ฐฐ์—ด์ด ๋‹ด๊น€
console.log(sign1); // ์–‘์ž๋ฆฌ
console.log(sign2); // ๋ฌผ๋ณ‘์ž๋ฆฌ
console.log(rest);  // [ 'ํ™ฉ์†Œ์ž๋ฆฌ', '๊ฒŒ์ž๋ฆฌ', '์‚ฌ์ž์ž๋ฆฌ' ]
/* 4. ๊ธฐ๋ณธ ๊ฐ’์„ ์„ค์ •ํ•˜๊ณ  ์‚ฌ์šฉ ๊ฐ€๋Šฅ */
let [firstName = "๋ฉ”๋กฑ", lastName = "ํ—ˆ"] = ["๊ผฌ์ˆœ"];
console.log(firstName); // ๊ผฌ์ˆœ
console.log(lastName);  // ํ—ˆ (๊ฐ’์ด ์ž…๋ ฅ๋˜์ง€ ์•Š์•˜์„ ๋•Œ์˜ ๊ธฐ๋ณธ๊ฐ’ ์ถœ๋ ฅ)

๐Ÿ‘‰ ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

let pants = {
    productName : '์กฐ๊ฑฐํŒฌ์ธ ',
    color : '์• ์‰ฌ๋ฒ ์ด์ง€',
    price : 25000 
};

let { productName, price, color } = pants;   // ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์˜ ๋ช…์นญ์„ ๊ธฐ์ค€์œผ๋กœ ๊ตฌ์กฐ ๋ถ„ํ•ด (ํ”„๋กœํผํ‹ฐ ์ž‘์„ฑ ์ˆœ์„œ ์ƒ๊ด€ X)
console.log(productName);
console.log(color);
console.log(price);

// ๊ฐ ๋ณ€์ˆ˜์˜ ์„œ์ˆ  ์ˆœ์„œ๋Š” ๋ฌด๊ด€ํ•˜๋ฉฐ { ๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ : ๋ชฉํ‘œ ๋ณ€์ˆ˜ } ํ˜•์‹์œผ๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ
let { color : co, price : pr, productName : pn } = pants;
console.log(co);
console.log(pr);
console.log(pn);

๐Ÿ’โ€ ๋‹ค์–‘ํ•œ ์‚ฌ์šฉ๋ฒ•

let pants = {
    productName : '์กฐ๊ฑฐํŒฌ์ธ ',
    color : '์• ์‰ฌ๋ฒ ์ด์ง€',
    price : 25000 
};
/* 1. ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋งŽ์€ ๋ณต์žกํ•œ ๊ฐ์ฒด์—์„œ ์›ํ•˜๋Š” ์ •๋ณด๋งŒ ๋ฝ‘์•„์˜ค๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉ */
let { productName } = pants;
console.log(`productName : ${productName}`);
/* 2. ๊ฐ์ฒด์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ”„๋กœํผํ‹ฐ๋Š” ๊ธฐ๋ณธ ๊ฐ’์„ ์„ค์ •ํ•ด์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ */
let shirts = {
    productName : '๋ฒ ์ด์ง ์…”์ธ '
};

let { productName : pn, color : co = '์–ด๋–ค ์ƒ‰์ƒ', price : pr = 0 } = shirts;
console.log(pn);    // ๋ฒ ์ด์ง ์…”์ธ 
console.log(co);    // ์–ด๋–ค ์ƒ‰์ƒ (๊ธฐ๋ณธ๊ฐ’)
console.log(pr);    // 0 (๊ธฐ๋ณธ๊ฐ’)
/* 3. rest parameter ...๋กœ ๋‚˜๋จธ์ง€ ์š”์†Œ๋ฅผ ํ•œ๋ฒˆ์— ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ */
let { productName : productName2, ...rest } = pants;
console.log(productName2);  // ์กฐ๊ฑฐํŒฌ์ธ 
console.log(rest);          // { color: '์• ์‰ฌ๋ฒ ์ด์ง€', price: 25000 }
/* 4. let ํ‚ค์›Œ๋“œ ์—†์ด ์‚ฌ์šฉํ•  ๋•Œ ์œ ์˜ํ•  ์  */
let productName3, color3, price3;

// { productName : productName3, color : color3, price : price3 } = pants;    // ์ฝ”๋“œ ๋ธ”๋Ÿญ์œผ๋กœ ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜ ๋ฐœ์ƒ
({ productName : productName3, color : color3, price : price3 } = pants);     // ์†Œ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์ฃผ๋ฉด ์˜ค๋ฅ˜ ํ•ด๊ฒฐ
console.log(productName3);
console.log(color3);
console.log(price3);

๐Ÿ’โ€ ์ค‘์ฒฉ ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น

let product = {
    size : {
        width : 10,
        height : 30
    },
    items : ['doll', 'robot']
};

// ์ค‘์ฒฉ ๊ตฌ์กฐ ๋ถ„ํ•ด ์‹œ ์ฝ”๋“œ๋ฅผ ์—ฌ๋Ÿฌ ์ค„์— ๊ฑธ์ณ ์ž‘์„ฑํ•ด ์˜๋„ํ•˜๋Š” ๋ฐ”๋ฅผ ๋ช…ํ™•ํžˆ ํ•จ
let {
    size : {
        width,
        height
    },
    items : [item1, item2],
    producer = "ํ—ˆ๊ฐ•๋ƒ‰"
} = product;

console.log(width);
console.log(height);
console.log(item1);
console.log(item2);
console.log(producer);

๐Ÿ’โ€ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ™œ์šฉ
ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ๋งŽ๊ฑฐ๋‚˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ธฐ๋ณธ๊ฐ’์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ํ™œ์šฉ

/* ์ธ์ˆ˜์˜ ์ˆœ์„œ๊ฐ€ ๊ณ ์ •๋˜์–ด์žˆ์–ด ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ๊ณ 
   ๊ธฐ๋ณธ ๊ฐ’ ํ™œ์šฉ ์‹œ์—๋„ undefined๋ฅผ ์ด์šฉํ•ด์„œ ์ธ์ˆ˜๋ฅผ ๋„˜๊ฒจ์ค˜์•ผ๋งŒ ํ•จ */
function displayProduct(producer = "๊ผฌ์ˆœ",  width = 0, height = 0, items = []) {}

displayProduct("์น˜์ฆˆ", undefined, undefined, ["Coffee", "Donut"]);
/* ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น์„ ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ ์šฉ */
function displayProduct2({producer = "์•„๋ฌด๊ฐœ", width = 0, height = 0, items = []}) {
    console.log(producer);  // ํ—ˆ๋ฉˆ๋จธ
    console.log(width);     // 0 
    console.log(height);    // 0
    console.log(items);     // [ 'Creame', 'Cheese' ]
}

// ํ•จ์ˆ˜์— ์ „๋‹ฌํ•  ๊ฐ์ฒด (์ผ๋ถ€)
let exampleProduct = {
    items : ["Creame", "Cheese"],
    producer : "ํ—ˆ๋ฉˆ๋จธ"
};

displayProduct2(exampleProduct);
profile
Tiny little habits make me

0๊ฐœ์˜ ๋Œ“๊ธ€