230410 - module(export, import)

๋ฐฑ์Šน์—ฐยท2023๋…„ 4์›” 10์ผ
1

๐Ÿšฉ javascript module

module

๐Ÿ“ ์„ค๋ช…

  • ๋ชจ๋“ˆ์ด๋ž€ .js ํŒŒ์ผ ํ•˜๋‚˜๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  • export์™€ import๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์™ธ๋ถ€ ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.


โœ’๏ธ ์‚ฌ์šฉ๋ฒ•

์ž…๋ ฅ

counter.js

export let count = 0;

export function increase() {
  count++;
  console.log(count);
}
// ์™ธ๋ถ€๋กœ ๋…ธ์ถœ์‹œํ‚ฌ ๋ถ€๋ถ„๋งŒ export ํ•ด์คŒ
// export default๋กœ ์„ค์ •๋˜์–ด ์žˆ์„ ๋•Œ๋Š” ๋‚ด ๋ง˜๋Œ€๋กœ ํ•จ์ˆ˜ ์ด๋ฆ„ ์ง€์ • ๊ฐ€๋Šฅ
// default ์—†์ด ๊ทธ๋ƒฅ export์ผ ๊ฒฝ์šฐ์—๋Š” ์ค‘๊ด„ํ˜ธ ์•ˆ์— ํ•จ์ˆ˜๋ช…์„ ๋„ฃ์–ด์„œ ์‚ฌ์šฉ (ํ•จ์ˆ˜ ์ด๋ฆ„ ์ง€์ • ๋ถˆ๊ฐ€)

// count๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ํ•จ์ˆ˜
export function getCount() {
  return count;
}

main.js

// import hi from "./counter.js";
// hi();
// counter.js ํŒŒ์ผ์—์„œ exportํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ export default๋กœ ์„ค์ •๋˜์–ด ์žˆ์„ ๋•Œ๋Š” ๋‚ด ๋ง˜๋Œ€๋กœ ํ•จ์ˆ˜ ์ด๋ฆ„ ์ง€์ • ๊ฐ€๋Šฅ

// import { increase } from "./counter.js";
// increase();
// default ์—†์ด ๊ทธ๋ƒฅ export์ผ ๊ฒฝ์šฐ์—๋Š” ์ค‘๊ด„ํ˜ธ ์•ˆ์— ํ•จ์ˆ˜๋ช…์„ ๋„ฃ์–ด์„œ ์‚ฌ์šฉ (ํ•จ์ˆ˜ ์ด๋ฆ„ ์ง€์ • ๋ถˆ๊ฐ€)

// import { increase as abc, getCount } from "./counter.js";
// ์›ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ ๋ฐ›์•„์˜ค๊ณ  ์‹ถ์„ ๋•Œ as ์‚ฌ์šฉ
// abc();
// abc();
// console.log("count๋Š”? : ", getCount());
// abc();

/*
import { increase, getCount } from "./counter.js";

increase();
increase();
increase();
const count = getCount();
console.log("getCountํ•จ์ˆ˜", count);
*/

import * as cc from "./counter.js";
// counter.js ์•ˆ์˜ ๋ชจ๋“  export ํ•ด์ฃผ๋Š” ์š”์†Œ๋“ค์„ cc๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๊ฐ€์ ธ์˜ด
cc.increase();
cc.increase();
cc.increase();

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






๐Ÿšฉ ์ถ”๊ฐ€ ์—ฐ์‚ฐ์ž

๋‹จ์ถ•ํ‰๊ฐ€. ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž (Logical Operator)

๐Ÿ“ ์„ค๋ช…

  • ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋Š” And(&&)์™€, Or(||)์ด ์žˆ๋‹ค.
  • ๋‹จ์ถ•ํ‰๊ฐ€(short-circuit evaluation) : if๊ตฌ๋ฌธ์—์„œ์ฒ˜๋Ÿผ ๋ชจ๋“  ํ–‰์ด ์‹คํ–‰๋˜์ง€ ์•Š์Œ


โœ’๏ธ ์‚ฌ์šฉ๋ฒ•

์ž…๋ ฅ

js

const obj1 = { name: "๐Ÿถ" };
const obj2 = { name: "๐Ÿฑ", owner: "๋ฐ”๋น„" };


if (obj1 && obj2) { // boolean ๊ฐ’์œผ๋กœ ๋ณ€ํ™”๋˜์–ด ํ‰๊ฐ€
  console.log("ใ…Žใ…‡");
}

// ์กฐ๊ฑด๋ฌธ ๋ฐ–์—์„œ ์‚ฌ์šฉ
result = obj1 || obj2;
console.log("|| ์กฐ๊ฑด๋ฌธ ๋ฐ–์—์„œ ์‚ฌ์šฉ", result);
// obj2๊ฐ€ return๋จ
console.log();

/*  
    - ๋‘˜ ๋‹ค true -    - obj1 false- 
  && : ๋’ค์˜ ๊ฐ’ ๋ฆฌํ„ด / ์•ž์˜ ๊ฐ’ ๋ฆฌํ„ด
  || : ์•ž์˜ ๊ฐ’ ๋ฆฌํ„ด / ๋’ค์˜ ๊ฐ’ ๋ฆฌํ„ด
*/

// ํ™œ์šฉ
function changeOwner(animal) { // ์ฃผ์ธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋งŒ ๋ณ€๊ฒฝ
  animal.owner = "์ผ„";
}
obj1.owner && changeOwner(obj1); // obj1์ด owner๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ์—๋งŒ ๋ณ€๊ฒฝ. ์•ž์ด true์ผ ๊ฒฝ์šฐ ๋’ค ์ฝ”๋“œ ์‹คํ–‰
obj2.owner && changeOwner(obj2);
console.log("obj1 changeOwner() ์‹คํ–‰ : ", obj1);
console.log("obj2 changeOwner() ์‹คํ–‰ : ", obj2);
console.log();

function makeNewOwner(animal) { // ์ฃผ์ธ์ด ์—†๋Š” ๊ฒฝ์šฐ์—๋งŒ ์ƒˆ ์ฃผ์ธ์„ ๋งŒ๋“ฆ
  animal.owner = "์ œ๋‹ˆํผ";
}
obj1.owner || makeNewOwner(obj1); // obj1์ด owner๊ฐ€ ์—†์„ ๊ฒฝ์šฐ์— ๋ณ€๊ฒฝ(์ถ”๊ฐ€)๋จ. ์•ž์ด false์ผ ๊ฒฝ์šฐ ๋’ค ์ฝ”๋“œ ์‹คํ–‰
obj2.owner || makeNewOwner(obj2); // ์•ž์ด true๋ผ ๋’ค ์ฝ”๋“œ ์‹คํ–‰โŒ
console.log("obj1 makeNewOwner() ์‹คํ–‰ : ", obj1);
console.log("obj2 makeNewOwner() ์‹คํ–‰ : ", obj2);

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

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