ES6

0taetaeยท2024๋…„ 10์›” 31์ผ
0
post-thumbnail

๐Ÿ“’ var, let, const

๐Ÿ“Œ scope

๐Ÿ’ก var vs let vs const

  • ์„ ์–ธ ๋ฐ ํ• ๋‹น
  • var : ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์„ ์–ธ ๋ฐ ์žฌํ• ๋‹น ๊ฐ€๋Šฅ
  • let : ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์žฌ์„ ์–ธ X, ์žฌํ• ๋‹น O
  • const : ๊ฐ’ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅ
    • ๊ฐ์ฒด { } ๋˜๋Š” ๋ฐฐ์—ด [ ]๋กœ ์„ ์–ธํ–ˆ์„ ๊ฒฝ์šฐ ๊ฐ์ฒด์˜ ์†์„ฑ๊ณผ ๋ฐฐ์—ด์˜ ์š”์†Œ๋Š” ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ

๐Ÿ’ก Scope

  1. ํ•จ์ˆ˜๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„ (function-level scope)
    • ํ•จ์ˆ˜ ๋‚ด์—์„œ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋Š” ํ•จ์ˆ˜ ๋‚ด์—์„œ๋งŒ ์œ ํšจ
    • ํ•จ์ˆ˜ ์™ธ๋ถ€์—์„œ๋Š” ์ฐธ์กฐ X
    • ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋Š” ์ง€์—ญ ๋ณ€์ˆ˜
    • ํ•จ์ˆ˜ ์™ธ๋ถ€์—์„œ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋Š” ๋ชจ๋‘ ์ „์—ญ๋ณ€์ˆ˜
  2. ๋ธ”๋ก๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„ (block-level scope)
    • ๋ชจ๋“  ์ฝ”๋“œ ๋ธ”๋ก(ํ•จ์ˆ˜, ์ œ์–ด๋ฌธ, ์กฐ๊ฑด๋ฌธ, try/catch๋ฌธ ๋“ฑ) ๋‚ด์—์„œ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋Š” ์ฝ”๋“œ ๋ธ”๋ก ๋‚ด์—์„œ๋งŒ ์œ ํšจํ•˜๋ฉฐ, ๋ธ”๋ก ์™ธ๋ถ€์—์„œ๋Š” ์ฐธ์กฐ X
    • ์ฝ”๋“œ ๋ธ”๋ก ๋‚ด๋ถ€์—์„œ ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋Š” ์ง€์—ญ ๋ณ€์ˆ˜
  • var์˜ ์œ ํšจ๋ฒ”์œ„ : ํ•จ์ˆ˜๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„
  • let์˜ ์œ ํšจ๋ฒ”์œ„ : ๋ธ”๋ก๋ ˆ๋ฒจ ์Šค์ฝ”ํ”„

๐Ÿ“’ Property Shorthand (๋‹จ์ถ• ์†์„ฑ๋ช…)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ ๊ฐ์ฒด์˜ key ๊ฐ’๊ณผ value์— ํ• ๋‹นํ•  ๋ณ€์ˆ˜๋ช…์ด ๊ฐ™์„ ๊ฒฝ์šฐ value๋ฅผ ์ƒ๋žต ๊ฐ€๋Šฅ
// ES6 ์ด์ „
const id = "test",
  name = "ํ™๊ธธ๋™",
  age = 30;

// const user = {
//   id: id,
//   name: name,
//   age: age,
// };
// console.log(user);

// ES6 ์ดํ›„

const user = {
    id,
    name,
    age
}
console.log(user)

๐Ÿ“’ Concise Method (๊ฐ„๊ฒฐํ•œ ๋ฉ”์†Œ๋“œ)

  • ๊ฐ์ฒด ๋‚ด์—์„œ ์ข€ ๋” ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉ
// ES6 ์ด์ „
const id = "test",
  name = "ํ™๊ธธ๋™",
  age = 30;

// const user = {
//   id: id,
//   name: name,
//   age: age,
  
//   info๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ต๋ช…ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ฆ
//   info: function () {
//     console.log("user info : " + this.id + " " + this.name + " " + this.age);
//   },
// };
// user.info();

// ํ•จ์ˆ˜ ์„ ์–ธ ํ˜•์‹
// function a() { } // ๋ช…๋ช…(์ต๋ช…) ํ•จ์ˆ˜ ์„ ์–ธ
// const a = function () { } // ๋ช…๋ช… ํ•จ์ˆ˜ ํ‘œํ˜„
// const a = new Function('x', 'y', 'return x + y') // ํ•จ์ˆ˜ ์ƒ์„ฑ์ž

// ES6 ์ดํ›„
const user = {
    id,
    name,
    age,
    info() {
        console.log("user info : " + this.id + " " + this.name + " " + this.age);
    }
}
user.info();

๐Ÿ“’ Destructuring Assignment(๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น)

  • ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด์— ์ž…๋ ฅ๋œ ๊ฐ’์„ ๊ฐœ๋ณ„์ ์ธ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๋Š” ๊ฐ„ํŽธํ•œ ๋ฐฉ์‹ ์ œ๊ณต
// ๋ฐฐ์—ด
const areas = ["๊ด‘์ฃผ", "๊ตฌ๋ฏธ", "์„œ์šธ", "๋Œ€์ „", "๋ถ€์šธ๊ฒฝ"];

// ES6 ์ด์ „
{
  const a1 = areas[0];
  const a2 = areas[1];
  const a3 = areas[2];
  const a4 = areas[3];
  const a5 = areas[4];

  console.log(a1, a2, a3, a4, a5);
}

// ES6 ์ดํ›„
{
  const [a1, a2, a3, a4, a5] = areas;
  console.log(a1, a2, a3, a4, a5);
}

// ๊ฐ์ฒด
const user = {
  id: "test",
  name: "ํ™๊ธธ๋™",
  age: 30,
};

// ES6 ์ด์ „
{
  let id = user.id;
  let name = user.name;
  let age = user.age;
  console.log(id, name, age);
}

// ES6 ์ดํ›„
// ๊ฐ์ฒด์˜ property์™€ ๋ณ€์ˆ˜๋ช…์ด ๊ฐ™์„ ๊ฒฝ์šฐ.
{
  let { id, name, age } = user;
  console.log(id, name, age);
}

// ๋ณ€์ˆ˜๋ช…์„ ๊ฐ์ฒด์˜ property๋ช…๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋งŒ๋“ค ๊ฒฝ์šฐ.
{
  let { id: userId, name: userName, age: userAge } = user;
  console.log(userId, userName, userAge);
}

// ๊ฐ์ฒด๋ฅผ return ํ•˜๋Š” ํ•จ์ˆ˜.
function getUser() {
  return {
    id: "test",
    name: "ํ™๊ธธ๋™",
    age: 30,
  };
}

// ES6 ์ด์ „
{
  let user = getUser();
  let id = user.id;
  let name = user.name;
  let age = user.age;
  console.log(id, name, age);
}

// ES6 ์ดํ›„
{
  let { id, name, age } = getUser();
  console.log(id, name, age);
}

// ES6 ์ด์ „
function showUserInfo1(user) {
  console.log("showUserInfo1 call");
  let id = user.id;
  let name = user.name;
  let age = user.age;
  console.log("์•„์ด๋”” : " + id);
  console.log("์ด๋ฆ„ : " + name);
  console.log("๋‚˜์ด : " + age);
}
showUserInfo1(user);

// ES6 ์ดํ›„
function showUserInfo2({ id, name, age }) {
  console.log("showUserInfo2 call");
  //let { id, name, age } = user;
  console.log("์•„์ด๋”” : " + id);
  console.log("์ด๋ฆ„ : " + name);
  console.log("๋‚˜์ด : " + age);
}
showUserInfo2(user);

๐Ÿ“’ Spread Syntax

  • Spread operator๋Š” ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์— ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•
  • ๋ฐฐ์—ด์ด๋‚˜ ๋ฌธ์ž์—ด ๋“ฑ์„ ํ’€์–ด์„œ ํ•˜๋‚˜ ํ•˜๋‚˜๋กœ ์ „๊ฐœ ๊ฐ€๋Šฅ
  • spread operator์˜ ๊ฒฝ์šฐ ๊ฐ’ ๋ณต์‚ฌ๊ฐ€ ์•„๋‹Œ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ๋ฐ”๊ฟ€๊ฒฝ์šฐ ๋ชจ๋‘ ๋ณ€๊ฒฝ
  • key๊ฐ€ ๊ฐ™์€ object๋ฅผ ๋ณ‘ํ•ฉํ•  ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ obj์˜ ๊ฐ’์ด ์„ค์ •
const user1 = { id: "test1" };
const user2 = { id: "test2" };

const arr = [user1, user2];

// array copy (reference)
const copyArr = [...arr];
console.log(arr, copyArr);
// ๊ฐ์ฒด์™€ ๋ฐฐ์—ด์€ === ๋น„๊ต์‹œ ์ฃผ์†Œ๋ฅผ ๋น„๊ต.
console.log(arr === copyArr); // false
// ๊ฐ์ฒด ๋˜๋Š” ๋ฐฐ์—ด์˜ ๋‚ด์šฉ ๋น„๊ต์‹œ JSON.stringify()์‚ฌ์šฉ
console.log(JSON.stringify(arr) === JSON.stringify(copyArr)); //true

// ์ฃผ์˜ : spread operator์˜ ๊ฒฝ์šฐ ๊ฐ’ ๋ณต์‚ฌ๊ฐ€ ์•„๋‹Œ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์„ ๋ฐ”๊ฟ€๊ฒฝ์šฐ ๋ชจ๋‘ ๋ณ€๊ฒฝ.
user1.id = "test";
console.log(arr, copyArr);

// ๋ฐฐ์—ด ๋ณต์‚ฌ + ์ƒˆ๋กœ์šด ๊ฐ์ฒด ์ถ”๊ฐ€.
const addArr = [...arr, { id: "ssafy3" }];
console.log(arr, addArr);

const team1 = ["์„œ์šธ", "๊ด‘์ฃผ"];
const team2 = ["๋Œ€์ „", "๋ถ€์šธ๊ฒฝ", "๊ตฌ๋ฏธ"];
// ๋ฐฐ์—ด ๋ณต์‚ฌ
const team3 = [...team1];
console.log(team3);
// ๋ฐฐ์—ด์˜ ์ถ”๊ฐ€ ์š”์†Œ๋กœ ๋„ฃ๊ธฐ
const team4 = ["์„œ์šธ", ...team2, "๊ด‘์ฃผ"];
console.log(team4);
// ๋‘ ๋ฐฐ์—ด ์ด์–ด ๋ถ™์ด๊ธฐ.
const team = [...team1, ...team2];
console.log(team);

//////////////////////////////////////////////////////

// ๊ฐ์ฒด ๋ณต์‚ฌ
const copyUser = { ...user1 };
// const refUser = user1;
// console.log(user1 === refUser); // true
console.log(copyUser);
copyUser.id = "test100";
console.log(user1, copyUser);
console.log(user1 === copyUser);

// ๊ฐ์ฒด ๋ณต์‚ฌ + ์ƒˆ๋กœ์šด property ์ถ”๊ฐ€.
const copyUser2 = { ...user1, name: "ํ™๊ธธ๋™" };
console.log(copyUser2);

// object merge (๋ณ‘ํ•ฉ)
const u1 = { id1: "test1" };
const u2 = { id2: "test2" };
const u = { ...u1, ...u2 };
console.log(u);

// ์ฃผ์˜์  : key๊ฐ€ ๊ฐ™์€ object๋ฅผ ๋ณ‘ํ•ฉํ•  ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ obj์˜ ๊ฐ’์ด ์„ค์ •.
const user = { ...user1, ...user2 };
console.log(user);

// ํ•จ์ˆ˜ ํ˜ธ์ถœ์‹œ ์ธ์ž๋กœ ์ „๋‹ฌ.
const num = [1, 3, 5, 7];

function plus(x, y, z) {
  return x + y + z;
}

const result = plus(...num);
console.log(result);

๐Ÿ“’ Default Parameter

  • ํ•จ์ˆ˜์— ์ „๋‹ฌ๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ undefined์ด๊ฑฐ๋‚˜ ์ „๋‹ฌ๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ, ์„ค์ •ํ•œ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
  • default parameter๊ฐ€ reference type์ผ ๊ฒฝ์šฐ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
// ES6 ์ด์ „
function print1(msg) {
  console.log(msg);
}
print1("hello");
print1();

// ES6 ์ดํ›„
function print2(msg = "์•ˆ๋…•") {
  console.log(msg);
}
print2("hello");
print2();

// default parameter๋Š” ํ•จ์ˆ˜์— ์ „๋‹ฌ๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ undefined์ด๊ฑฐ๋‚˜ ์ „๋‹ฌ๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ, ์„ค์ •ํ•œ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”.
function getUserId(userId = "test") {
  return userId;
}

console.log(getUserId());
console.log(getUserId(undefined));
console.log(getUserId(null));
console.log(getUserId("hissam"));

// default parameter๊ฐ€ reference type์ผ ๊ฒฝ์šฐ ํ˜ธ์ถœ๋  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•จ.
function appendArr(val, array = []) {
  array.push(val);
  return array;
}

console.log(appendArr(10)); // [10]
console.log(appendArr(20)); // [20] or [10, 20] ???

๐Ÿ“’ Template String

  • ๋ฌธ์ž์—ด์— ๋ณ€์ˆ˜๋ฅผ ํฌํ•จ์‹œํ‚ฌ ๋•Œ ์ข€ ๋” ์ง๊ด€์ ์ด๊ณ  ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋Šฅ
  • ๋ณ€์ˆ˜๋ฅผ ๋„ฃ๊ณ ์ž ํ•˜๋Š” ๋ถ€๋ถ„์— ${var} ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์–ด ์คŒ
  • ๋ฌธ์ž์—ด ์‚ฌ์šฉ์‹œ ํฐ๋”ฐ์˜ดํ‘œ ๋Œ€์‹  ๋ฐฑํ‹ฑ ์‚ฌ์šฉ
  • ๋ฐฑํ‹ฑ ๋‚ด๋ถ€์˜ ์ค„ ๋ฐ”๊ฟˆ์€ ๊ทธ๋Œ€๋กœ ์ ์šฉ
const id = "test",
  name = "ํ™๊ธธ๋™",
  age = 30;
// ES6 ์ด์ „
console.log(name + "(" + id + ")๋‹˜์˜ ๋‚˜์ด๋Š” " + age + "์ž…๋‹ˆ๋‹ค.");
// ES6 ์ดํ›„
console.log(`${name}(${id})๋‹˜์˜ ๋‚˜์ด๋Š” ${age}์ž…๋‹ˆ๋‹ค`);

๐Ÿ“’ Arrow Function

  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜
๐Ÿ“Œ

Java์˜ lambda์‹ vs JS์˜ Arrow Function โ†’ this

์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ this, addeventlistener์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ this ๋“ฑ

  • ํ•จ์ˆ˜ ์„ ์–ธ
    // ๊ธฐ์กด ํ•จ์ˆ˜ ์„ ์–ธ
    function name(param) {}
    
    // ํ™”์‚ดํ‘œ ํ•จ์ˆ˜
    // ํ•จ์ˆ˜์˜ ์ด๋ฆ„์ด ์—†๋Š” ์ต๋ช…ํ•จ์ˆ˜ ์ด๋ฏ€๋กœ ๋ฐ˜๋“œ์‹œ ๋ณ€์ˆ˜์— ๋‹ด์•„์„œ ์‚ฌ์šฉ.
    const name = (param) => {};
  • ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋”ฐ๋ฅธ ์„ค์ •
    • ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†์„ ๊ฒฝ์šฐ
      • ( ) โ‡’ {} ;
    • ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•œ ๊ฐœ ์žˆ์„ ๊ฒฝ์šฐ, (param)์˜ ์†Œ๊ด„ํ˜ธ ์ƒ๋žต ๊ฐ€๋Šฅ
      • (param) โ‡’ {} ;
    • ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์„ ๊ฒฝ์šฐ, (param1, param2)์˜ ์†Œ๊ด„ํ˜ธ ์ƒ๋žต X
      • (param1, param2) โ‡’ {} ;
  • function body ์„ค์ •
    (x) => {
       return x + 10;
    };
    • body์˜ ๋‚ด์šฉ์ด ํ•œ์ค„์ผ ๊ฒฝ์šฐ {} ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๊ณ  ์ž๋™์œผ๋กœ ๊ฒฐ๊ณผ๊ฐ’์ด return๋œ๋‹ค.
      (x) => x + 10;
      
    • object return์‹œ ( ) ์‚ฌ์šฉ.
      () => {
      	return { id: "test" };
      };
      
      () => ({ id: "test" });
    • body๊ฐ€ ์—ฌ๋Ÿฌ์ค„์ผ ๊ฒฝ์šฐ {}, return ์ƒ๋žต ๋ถˆ๊ฐ€.
      (x) => {
      	const y = x + 100;
      	return y;
      };
// ES6 ์ด์ „
function func1() {
  return 100;
}
let result = func1();
console.log(result);

// ES6 ์ดํ›„
//const func2 = () => { return 100; }
const func2 = () => 100;
let result2 = func2();
console.log(result2);

// ES6 ์ด์ „
function func3(x) {
  return x + 100;
}
result = func3(50);
console.log(result);

// ES6 ์ดํ›„
const func4 = (x) => x + 100;
result = func4(50);
console.log(result);

// ES6 ์ด์ „
function func5(x, y) {
  return x + y;
}
result = func5(50, 80);
console.log(result);

// ES6 ์ดํ›„
const func6 = (x, y) => x + y;
result = func6(50, 80);
console.log(result);

// ES6 ์ด์ „
function func7() {
  return {
    id: "test",
  };
}
result = func7();
console.log(result.id);

// ES6 ์ดํ›„
const func8 = () => ({ id: "test" });
result = func8();
console.log(result.id);

// ES6 ์ด์ „
function func9(x) {
  const y = x * 3;
  return y;
}
result = func9(10);
console.log(result);

// ES6 ์ดํ›„
const func10 = x => {
  const y = x * 3;
  return y;
}
result = func10(10);
console.log(result);

// Arrow Function์—์„œ๋Š” this๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋˜์ง€ ์•Š์Œ.
const user = {
  id: "test",
  age: 20,
  //   info() {
  //     console.log(this.id, this);
  //   },
  // info: () => console.log(this.id, this), // this ์‚ฌ์šฉ ๋ถˆ๊ฐ€.
  info: function () {
    const innerFunc = () => console.log(this.id, this);
    innerFunc();
  }
};
user.info();

๐Ÿ“’ Module

๐Ÿ’ก Module

  • import ๋˜๋Š” export ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉ
<script type="module" src="app.js"></script>

๐Ÿ’ก module system

  • CommonJS (NodeJS) : ์›น ๋ธŒ๋ผ์šฐ์ € ๋ฐ–์—์„œ๋„ ๋™์ž‘๋  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“ˆ ๊ทœ์น™ ์„ค๋ฆฝ
  • AMD (Asynchronous Module Definition) : ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋ชจ๋“ˆ์„ ๋กœ๋”ฉ
  • ESM (ECMAScript Module, ECMA215, es6) : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ž์ฒด ๋ชจ๋“ˆ

๐Ÿ’ก module scope

  • ๋ชจ๋“ˆ์˜ ๊ฐ€์žฅ ๋ฐ”๊นฅ์ชฝ์—์„œ ์„ ์–ธ๋œ ์ด๋ฆ„์€ ์ „์—ญ ์Šค์ฝ”ํ”„๊ฐ€ ์•„๋‹ˆ๋ผ ๋ชจ๋“ˆ ์Šค์ฝ”ํ”„์—์„œ ์„ ์–ธ๋จ
  • ๋ชจ๋“ˆ ์Šค์ฝ”ํ”„์— ์„ ์–ธ๋œ ์ด๋ฆ„์€ export ํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ํ•ด๋‹น ๋ชจ๋“ˆ ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅ โ†’ ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ์˜ ๊ฐ€์žฅ ๋ฐ”๊นฅ์ชฝ์—์„œ ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•˜๋”๋ผ๋„, ์„œ๋กœ ๋‹ค๋ฅธ ์Šค์ฝ”ํ”„์—์„œ ์„ ์–ธ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฆ„์˜ ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ

๐Ÿ’ก export & import

  • module scope์— ์„ ์–ธ๋œ ์ด๋ฆ„์€ export ๊ตฌ๋ฌธ์„ ํ†ตํ•ด ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
    • ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ํด๋ž˜์Šค export ๊ฐ€๋Šฅ

    • ๊ฐœ๋ณ„ export, ํ•œ๋ฒˆ์— export, export default

      export const name = "ํ™๊ธธ๋™";
      export {name, age, info, Student };
      export default function() {}
  • exort ๋œ ์ด๋ฆ„์„ ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ import ๊ตฌ๋ฌธ์„ ํ†ตํ•ด ๊ฐ€์ ธ์˜จ ๋’ค ์‚ฌ์šฉ ๊ฐ€๋Šฅ
<script type="module">
      import { name, age, info, Student } from "./02.declare.js";

      console.log(name, age);

      const result = info();
      console.log(result);
</script>
  1. export๋ฅผ ์ด์šฉํ•œ ๊ฐœ๋ณ„ ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ํด๋ž˜์Šค๋ฅผ export

    • ์ด๋ฆ„์„ ์„ ์–ธํ•˜๋Š” ๊ตฌ๋ฌธ ์•ž์— export๋ฅผ ๋ถ™์ด๋ฉด ์„ ์–ธ๊ณผ ๋™์‹œ์— export ๊ฐ€๋Šฅ
    export const name = "ํ™๊ธธ๋™";
    export const age = 30;
    
    export function info() {
      return `์ด๋ฆ„ : ${name}, ๋‚˜์ด : ${age}`;
    }
    
    export class Student {}

    โ‡’ ํ™๊ธธ๋™ 30

    ์ด๋ฆ„ : ํ™๊ธธ๋™, ๋‚˜์ด : 30

  1. export๋ฅผ ์ด์šฉํ•œ ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ํด๋ž˜์Šค๋ฅผ ํ•œ๋ฒˆ์— export

    • ํŒŒ์ผ์˜ ๋งˆ์ง€๋ง‰์— export๋ฅผ ์„ ์–ธํ•˜์—ฌ ํ•œ๋ฒˆ์— export ๊ฐ€๋Šฅ
    const name = "ํ™๊ธธ๋™";
    const age = 30;
    
    function info() {
      return `์ด๋ฆ„ : ${name}, ๋‚˜์ด : ${age}`;
    }
    
    class Student {}
    
    export { name, age, info, Student };
  1. export default
    • ๋ชจ๋“ˆ์„ ๋Œ€ํ‘œํ•˜๋Š” ํ•˜๋‚˜์˜ ๊ฐ’์„ ์ง€์ •ํ•˜๊ณ  ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ { }๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋จ
      export const name = "ํ™๊ธธ๋™";
      export const age = 30;
      
      export default function () {
        return `์ด๋ฆ„ : ${name}, ๋‚˜์ด : ${age}`;
      }
      
      export class Student {}
    • import ์‹œ ์ด๋ฆ„์„ ๋‹ค๋ฅด๊ฒŒ ๊ฐ€๋Šฅ
      <script type="module">
            import info2, { name, age, Student } from "./04.declare.js";
      
            console.log(name, age);
      
            const result = info2();
            console.log(result);
      </script>
    • ๊ฐ์ฒด ํ˜•์‹์œผ๋กœ ํ•˜๊ฒŒ ๋˜๋ฉด ํ•œ๋ฒˆ์— import ๊ฐ€๋Šฅ

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