Animated Rainbow Nyan Cat
firstNumber: 100,
secondNumber:20,
print: function() {
    console.log(this);
    function add() {
        console.log(this);
        return this.firstNumber + this.secondNumber;
    }
    console.log(this.firstNumber + "+" + this.secondNumber + "=" + add());
}

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ํ•จ์ˆ˜ ์•ˆ์— ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ์˜ this๋Š” ์ž๊ธฐ์ž์‹  ์ฐธ์กฐ.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ํ”„๋กœํ† ํƒ€์ž… ์ง€์›.. add()์•ˆ์˜ this๋Š” window๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ด์„œ this.firstNumber๋กœ ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ค์ง€ ๋ชปํ•œ๋‹ค.

firstNumber: 100,
secondNumber:20,
print: function() {
    let self = this;
    function add() {
        console.log(self);
        return self.firstNumber + self.secondNumber;
    }
    console.log(this.firstNumber + "+" + this.secondNumber + "=" + add());
}

์ด๋Ÿฐ์‹์œผ๋กœ ๋‹ด์•„์„œ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. self๋กœ

๋˜๋Š”,

firstNumber: 100,
secondNumber:20,
print: function() {
    const add = () => {
        console.log(this);
        return this.firstNumber + this.secondNumber;
    }
    console.log(this.firstNumber + "+" + this.secondNumber + "=" + add());
}

์ด๋Ÿฐ์‹์œผ๋กœ arrow function์œผ๋กœ ํ•ด์ค„์ˆ˜๋„ ์žˆ๋‹ค.

 

of ๋’ค์—๋Š” iterableํ•œ ๊ฐ์ฒด๋งŒ ์˜ฌ์ˆ˜ ์žˆ๋‹ค.

in์—๋Š” ์–ด๋–ค ๊ฐ์ฒด๋“  ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ์—ˆ์œผ๋‚˜..

 

let str = "๊ฐ€๋‚˜๋‹ค๋ผ๋งˆ";
		
for(let ch of str) {
    console.log(ch);
}
let ary = [...str];
console.log(ary);
const [c1, c2] = str;		
const [c3, c4, ...c5] = str;

์ด๋Ÿฐ์‹์œผ๋กœ ํ•œ๋‹ค๋ฉด, c1์—๋Š” '๊ฐ€', c2์—๋Š” '๋‚˜', c3์—๋Š” '๊ฐ€',c4์—๋Š” '๋‚˜', c5์—๋Š” '๋‹ค','๋ผ','๋งˆ' ๊ฐ€ ๋“ค์–ด๊ฐ„๋‹ค.

 

let coffee = {
    name : "Americano",
    price : 3000
};
let {name: coffeeName2, caffeine: coffeeCaffeine1 = 500} = coffee;
console.log(coffeeName2);
console.log(coffeeCaffeine1);

name์„ coffeeName2์— ํ• ๋‹นํ•˜๊ณ , 500์„ coffeeCaffeine1์— ํ• ๋‹นํ•ด๋ผ..

์ฝ˜์†”์ฐฝ์—๋Š” ์ด๋Ÿฐ์‹์œผ๋กœ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

str = `${firstNumber1} * ${secondNumber1} = ${firstNumber1 * secondNumber1}`;
console.log(str);

` ๋ฌธ์ž์—ด์—์„œ๋Š” ๊ฐœํ–‰๋ฌธ์ž, ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ๋‹ค ์ธ์‹๋œ๋‹ค

const colorArray = ["red", "green", "blue"];
const newColorArray = [...colorArray, "orange"];
		
console.log(colorArray);		
console.log(newColorArray);

์ฝ˜์†”์ฐฝ์— ์ด๋Ÿฐ์‹์œผ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

 

function myPrint(a, b, c) {
    console.log(`a=${a}`);
    console.log(`b=${b}`);
    console.log(`c=${c}`);
}
const array = [1, 2, 3];		
console.log(array);
myPrint( ... array);
		
const stringArray = [ ... "xyz"];		
console.log(stringArray);		
myPrint( ... stringArray);

...์„ ๋ถ™์ด๋ฉด ์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์—

์ด๋Ÿฐ ์ฝ˜์†”์ฐฝ์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

const array1 = ["a", "b", "c"];
console.log(array1);
array1.push("d");
console.log(array1);
array1.push("e", "f");
console.log(array1);
array1.push( ... ["g", "h", "i"]);		
console.log(array1);
array1.push(["j", "k"]);		
console.log(array1);

์Šคํ”„๋ ˆ๋“œ ์‹์œผ๋กœ ๋ณด๋‚ด์ง€ ์•Š์œผ๋ฉด ๊ทธ๋Œ€๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

๊ทธ๋ž˜์„œ ์œ„์™€ ๊ฐ™์€ ์ฝ˜์†”์ฐฝ์ด ๋ณด์ธ๋‹ค.

 

const array2 = [10, 20, 60, 50, 30];
console.log(Math.max( ... array2));
console.log(Math.max(array2));

๋ฐฐ์—ด์— ๋‹ด์•„์„œ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๊ตฌํ•  ์ˆ˜ ์—†๋‹ค..

์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ธ๋‹ค.

์Šคํ”„๋ ˆ๋“œ ์—ฐ์‚ฐ์ž๋กœ ํ’€์–ด์„œ ์ „๋‹ฌํ•ด์•ผ ํ•œ๋‹ค.

const coffee = {
    name : "Americano",
    price : 3000
};
const coffee2 = {
    "name" : "Americano",
    "price" : 3000
};
const coffee3 = {
    'name' : 'Americano',
    'price' : 3000
};

์ด๋ ‡๊ฒŒ ์ธ์šฉ๋ถ€ํ˜ธ ์žˆ์–ด๋„ ๋˜๊ณ  ์—†์–ด๋„ ๋จ. ๊ทธ๋Ÿฐ๋ฐ ์—†์œผ๋ฉด - ์ด๊ฑฐ๋Š” ๋ชป์“ด๋‹ค.

const key1 = "name";
const key2 = "price";
		
const coffee4 = {
    key1 : 'Americano',
    key2 : 3000
};

์ด๋Ÿฐ์‹์œผ๋กœ ํ•˜๋ฉด key1์€ name์ด๋ผ๋Š” ์†์„ฑ๋ช…์ด ์•„๋‹˜. ๊ทธ๋ƒฅ key1์ด๋ผ๋Š” ์†์„ฑ๋ช….

๊ทธ๋Ÿฌ๋‚˜

const coffee5 = {
    [key1] : 'Americano',
    [key2] : 3000
};

์ด๋Ÿฐ์‹์œผ๋กœ ๋Œ€๊ด„ํ˜ธ๋ฅผ ๋ถ™์ด๋ฉด ์†์„ฑ๋ช…์œผ๋กœ ์“ธ์ˆ˜ ์žˆ๋‹ค. ์œ„์—์„œ key1์€ name์œผ๋กœ ์ •ํ•ด์คฌ์œผ๋ฏ€๋กœ [key1]์€ name์ด๋ผ๋Š” ์†์„ฑ๋ช…์„ ๊ฐ–๊ฒŒ ๋œ๋‹ค.

 

const coffee = {
    name : name,
    price : price
};		
const coffee2 = {
    name,
    price
};		
const coffee3 = {name,price};

์†์„ฑ๋ช…๊ณผ ๋ณ€์ˆ˜๊ฐ€ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์œ„์˜ ์„ธ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

 

const obj = {
    x : 10, y : 20,
    test2() {
        return this.x + this.y;
    },

x,y,test2์€ obj์˜ ์†์„ฑ์ด๋‹ค, x,y๋Š” ๋ณ€์ˆ˜ ์‚ฌ์šฉํ•˜๋“ฏ์ด, test2์€ ๋ฉ”์„œ๋“œ.

function์„ ๊ตณ์ด ๋ถ™์—ฌ์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.function์„ ๋ถ™์—ฌ์ค€๋‹ค๋ฉด test2 : function(){ ์ด๋Ÿฐ์‹์œผ๋กœ ํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ..

 

test4(p) {
    p ??= 3; // p๊ฐ€ null ์ด๊ฑฐ๋‚˜ undefined  ์ด๋ฉด 3์„ ํ• ๋‹น
    return p+p; 
}

??= ์ด๊ฑฐ๋Š” p๊ฐ€ null ์ด๊ฑฐ๋‚˜ undefined์ด๋ฉด 3์„ ํ• ๋‹นํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

class Student {
    constructor(name, korean, math, english, science) {
        this.์ด๋ฆ„ = name;
        this.๊ตญ์–ด = korean;
        this.์ˆ˜ํ•™ = math;
        this.์˜์–ด = english;
        this.๊ณผํ•™ = science;
    } 
				
    toString() {
        return this.์ด๋ฆ„ + " :  " +
            this.๊ตญ์–ด + ", " +
            this.์ˆ˜ํ•™ + ", " +
            this.์˜์–ด + ", " +
            this.๊ณผํ•™
    }
}

์ด๋Ÿฐ์‹์œผ๋กœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ˆ˜ ์žˆ๋‹ค.

class Person {
    // constructor(์ƒ์„ฑ์ž)
    constructor(name, age) { 
        // fields
        this.name = name; 
        this.age = age;
    }
    // methods
    speak(color) {
        writeColor(`${this.name} ์•ˆ๋…•!`, 'h3', color);
    }
}
let p1 = new Person('๋‘˜๋ฆฌ', 10);
p1.speak('green');

์ด๋ ‡๊ฒŒ ํ•œ๋‹ค๋ฉด ์ฝ˜์†”์ฐฝ์—๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถœ๋ ฅ๋˜๊ฒŒ ๋œ๋‹ค.

class User {
    constructor(firstName, lastName, age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    get age() {
        write('getter ํ˜ธ์ถœ', 'h4');
        return this._age;
    }

    set age(value) {
        write('setter ํ˜ธ์ถœ : ' + value, 'h4');
        this._age = value < 0 ? 0 : value; 
    }

    toString() {
        return `${this.lastName} ${this.firstName} : ${this.age}์„ธ`;
    }
}

์ด๋Ÿฐ์‹์œผ๋กœ getter,setter ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. age์— ๋Œ€ํ•œ getter์™€ setter๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

let obj = new User('๊ธธ๋™', '๊ณ ', 20);
obj.age = 40;

๊ทธ๋ฆฌ๊ณ  obj.age=40 ์ด๋Ÿฐ์‹์œผ๋กœ ํ•˜๋ฉด ์ž๋™์œผ๋กœ setter๊ฐ€ ํ˜ธ์ถœ๋˜๊ฒŒ ๋œ๋‹ค.

_๊ฐ€ ์—†์œผ๋ฉด getter๊ฐ€ ๊ณ„์†ํ•ด์„œ ํ˜ธ์ถœ๋˜๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์— _๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค. ๊ฒŒํ„ฐ, ์„ธํ„ฐ ์„ค์ •ํ•  ๋•Œ.( this. age๋ผ๊ณ  ํ•˜๋ฉด rvaule์ผ๋•Œ๋Š” getter๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  lvaule์ผ๋•Œ๋Š” setter๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ.)

class Shape {
    constructor(width, height, color) {
        this.width = width;
        this.height = height;
        this.color = color;
    }
    draw() {
        write(`${this.color} ์ƒ‰์ƒ์œผ๋กœ ๋„ํ˜•์„ ๊ทธ๋ฆฐ๋‹ค`, 'h3');
    }
    getArea() {
        return this.width * this.height;
    }
}
class Rectangle extends Shape {} 
class Triangle extends Shape {
    getArea() {
        return (this.width * this.height) / 2;
    }
    draw() {
        super.draw(); // super๋Š” ๋ถ€๋ชจํด๋ž˜์Šค, ์ฆ‰ Shapeํด๋ž˜์Šค๋ฅผ ์ง€์นญํ•œ๋‹ค.
    }
}

ํ”„๋กœํ† ํƒ€์ž…์„ ํ†ตํ•œ ๊ฐ์ฒด ์ง€ํ–ฅ์–ธ์–ด๋‹ค.. ์ž๋ฐ”๋กœ ๋งŒ๋“ค์–ด์ง€๋Š” ๋ชจ๋“  ๊ฐ์ฒด๋Š” Object์˜ ์ž์‹์ด๊ฑฐ๋‚˜ ์ž์†์ด ๋œ๋‹ค.

Object๋ฅผ ๋ณต์ œํ•ด์„œ ์ •๋ณด๋ฅผ ๋„ฃ๋Š”๋‹ค.

 

์ปจํŠธ๋กค๋Ÿฌ์—์„œ cors ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด

@CrossOrigin(origins = "*")

์ด๋Ÿฐ ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

 

const ajaxrequest = {
    get(url) {
        return fetch(url);
    },
    post(url, payload) {
        return fetch(url, {
            method: 'POST',
            headers: { 'content-Type': 'application/json' },
            body: JSON.stringify(payload)
        });
    },
    delete(url) {
        return fetch(url, { method: 'DELETE' });
    }
}

ํ˜•์‹์ด json์ด๋ผ๊ณ  ํ—ค๋”์—์„œ ์•Œ๋ ค์ค€๋‹ค. fetch๋กœ ํ†ต์ผ๋˜์–ด ์žˆ๋Š”๋ฐ, ๋‘๋ฒˆ์งธ ์•„๊ทœ๋จผํŠธ์— ์ •๋ณด๋ฅผ ๋‹ด์€ ๊ฑธ ์ฃผ๋ฉด์„œ ์—ฌ๋Ÿฌ๊ฐ€์ง€๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

function getAll() {
    ajaxrequest.get('http://localhost:8088/boards')
        .then(response => {
            if (!response.ok) throw new Error(response.statusText);
                return response.json();
        })
        .then(boards => console.log(boards))
        .catch(err => console.error(err));
}

chaining ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„.. then์ด๋‚˜ catch๋‚˜.. 

ํ˜ธ์ถœ๋˜๋Š” ์ƒํ™ฉ์„ ์ฝœ๋ฐฑํ•œ๋‹ค๋ผ๊ณ  ํ•จ. ajax์š”์ฒญ์ด ์„ฑ๊ณต์ ์œผ๋กœ ๋๋‚ฌ์„๋•Œ thenํ˜ธ์ถœ ์—๋Ÿฌ๋‚ฌ์„๋•Œ๋Š” catch ํ˜ธ์ถœ

์‘๋‹ต์ด ์˜ค๊ธดํ–ˆ๋Š”๋ฐ 200์ด ์•„๋‹๊ฒฝ์šฐ (404์ด๊ฑฐ๋‚˜..) if (!response.ok) ์ด๋Ÿฐ ์กฐ๊ฑด๋ฌธ์„ ๊ฑธ์–ด์ค€๋‹ค.

 

.json()ํ•˜๋ฉด ์‘๋‹ตํ•œ json ํ˜•์‹์„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค. ๊ทธ๊ฑธ ํ”„๋กœ๋ฏธ์Šค ํ˜•์‹์œผ๋กœ ์‘๋‹ตํ•ด์ฃผ๊ธฐ
๋•Œ๋ฌธ์— then์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋Š”๊ฑฐ..

function sum(num1, num2) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            var result = num1 + num2;
            console.log(num1 + " + " + num2 + " = ");
            resolve(result);
        }, 2000);
    });
}

์–ธ์ œ๋๋‚ ์ง€ ๋ชจ๋ฅด๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ๋ฐ›๋Š”๋ฐ ์„ฑ๊ณต์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๋ฉด resolve, ์‹คํŒจํ•˜๋ฉด reject. 2์ดˆํ›„์— ์ˆ˜ํ–‰ํ•˜๊ฒ ๋‹ค.

const res1 =  sum(100, 200);
console.log(res1);

promise๊ณ  ๋ญ๊ณ  ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๋Š” ๊ฒŒ ๋‹ค์•ผ. ๋ผ๋Š” ๋œป. ํ˜ธ์ถœํ• ๋•Œ๋Š”..

์ด๋Ÿฌ๋ฉด ์ฝ˜์†”์ฐฝ์—๋Š” 

์ด๋Ÿฐ์‹์œผ๋กœ ๋œฌ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜

function processAsync() {
    sum(100, 200)
    .then(res1 => {
        console.log(res1)
        minus(100, 200)
        .then (res1 => {
            console.log(res1);
            multiply(100, 200)				
            .then(res1 => {
                console.log(res1);
                divide(100, 200)
                .then(res1 => console.log(res1));
            });
        });
    });			
}

์ด๋Ÿฐ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์ˆœ์„œ๋Œ€๋กœ ๋œจ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฒด์ด๋‹ ๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ๋‹ค. ์‘๋‹ต์ด ์˜ค๋ฉด ์ฒ˜๋ฆฌํ•˜๊ณ  ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•˜๊ณ  ์žˆ๋‹ค. ์ด๋Ÿฐ์‹์œผ๋กœ ๋น„๋™๊ธฐ์ฒ˜๋ฆฌ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค. ์ด๋Ÿฐ ํ”„๋กœ๋ฏธ์Šค ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด์„œ. ๊ทธ๋Ÿฌ๋‚˜ ์ฝœ๋ฐฑ์˜ ์ง€์˜ฅ

์ฝ˜์†”์ฐฝ์— ์ด๋Ÿฐ์‹์œผ๋กœ ๋œฌ๋‹ค... 

async function processAsync() {
    const res1 = await sum(100, 200);
    console.log(res1);
    const res2 = await minus(100, 200);
    console.log(res2);
    const res3 = await multiply(100, 200);
    console.log(res3);
    const res4 = await divide(100, 200);
    console.log(res4);
}

async await ๊ตฌ๋ฌธ. ์•ž์— await๋ฅผ ๋ถ™์ด๋ฉด sum์ด ์–ธ์ œ๋๋‚ ์ง€๋Š” ๋ชจ๋ฅด์ง€๋งŒ ๋๋‚ ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•œ๋‹ค๋Š” ๋œป. ๊ทธ๋ฆฌ๊ณ  ๋ฆฌํ„ด์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค. ์œ„์™€ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. ์ด await๋Š” ๋ฐ˜๋“œ์‹œ async function ์•ˆ์—์„œ๋งŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์†Œ์Šค ํ•˜๋‚˜๋ฅผ ํ•˜๋‚˜์˜ ๋ชจ๋“ˆ๋กœ ๋ด„. ๊ฐ™์€ ์ด๋ฆ„์˜ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋˜, ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋˜ ๋ชจ๋“ˆ๋กœ ๋ณด๋‹ˆ๊นŒ ์กฐ๊ธˆ ๋” ์ž์œ ๋กœ์›Œ์ง.

 

export ์ง€์‹œ์ž : ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜ ์•ž์— ๋ถ™์ด๋ฉด ์™ธ๋ถ€ ๋ชจ๋“ˆ์—์„œ ํ•ด๋‹น ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. -> ๋ชจ๋“ˆ ๋‚ด๋ณด๋‚ด๊ธฐ

import ์ง€์‹œ์ž : ์‚ฌ์šฉํ•˜๋ฉด ์™ธ๋ถ€ ๋ชจ๋“ˆ์˜ ๊ธฐ๋Šฅ์„ ํ˜„์žฌ ๋ชจ๋“ˆ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 

์ถœ์ฒ˜ : https://www.samanthaming.com/tidbits/79-module-cheatsheet/

- ๋ชจ๋“ˆ ๋‚ด๋ณด๋‚ด๊ธฐ

export component1
export component2
..
export {component1,component2,..}

๋˜‘๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ importํ•ด์•ผ๋จ

 

(๊ธฐ๋ณธ ๋‚ด๋ณด๋‚ด๊ธฐ)

export defalut component_name

 

์ด๋ฆ„ ์ƒ๊ด€์—†์ด importํ•  ์ˆ˜ ์žˆ๋‹ค.

 

- ๋ชจ๋“ˆ ๊ฐ€์ ธ์˜ค๊ธฐ

import {component1, component2..} from module_name
import {original_component_name as new_component_name}
import * as variable_name from module_name

 

(๊ธฐ๋ณธ ๋‚ด๋ณด๋‚ด๊ธฐ ๊ฐ€์ ธ์˜ฌ ๋•Œ)

import any_variable_name from module_name

 

let name = "๊ธ”๊ธ”";

let coffee = {
    getName:function(){
        return name
    },
    setName:function(newName){
        name = newName
    }
};

export default coffee;

submodule2.js ๋ฅผ ์ด๋Ÿฐ์‹์œผ๋กœ ์ž‘์„ฑํ•˜๊ณ  export default๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด

import ky from "./submodule2.js";
console.log(ky.getName());
ky.setName("์˜๊ธ”");
console.log(ky.getName());

mainmodule4.js๋ฅผ ์ด๋Ÿฐ์‹์œผ๋กœ ์ž‘์„ฑํ•ด์ค„์ˆ˜์žˆ๋‹ค. import ky ์ด๋Ÿฐ์‹์œผ๋กœ ์•„๋ฌด ์ด๋ฆ„์ด๋‚˜ importํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฝ˜์†”์ฐฝ์— ์ด๋Ÿฐ์‹์œผ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

mainmodule์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ค„์ˆ˜๋„ ์žˆ๋‹ค.

import ky, {name} from "./submodule3.js";

console.log(name);
console.log(ky.getName());

ky.setName("์˜๊ธ”");
console.log(ky.getName());

์œ„์™€ ๊ฐ™์€ ์ฝ˜์†”์ฐฝ์„ ๋ณด์ด๊ฒŒ ๋œ๋‹ค.

'๐Ÿ’ผ Full-Stack Study โ‘ก (feat. KOSA)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Vue.js (watch)  (0) 2023.11.08
Vue.js  (0) 2023.11.06
Spring Security, ES6  (1) 2023.11.03
JPA, ResponseEntity, HATEOAS  (0) 2023.11.02
REST API, URI  (0) 2023.11.01