JavaScript - The Complete Guide 2022

Section 20: JavaScript Modules - Writing Modular (=Multi-File) Code

olivia_yj 2022. 11. 3. 22:21

The goals

๐Ÿ’ช๐ŸปWhat & Why?

โœŒ๐ŸปUsage

 

Serve

sudo npm install -g serve

 

Module

๋ชจ๋“ˆ์ด๋ž€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐœ๋ณ„์  ์š”์†Œ๋กœ์„œ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ฝ”๋“œ ์กฐ๊ฐ์„ ๋งํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ๋ชจ๋“ˆ์€ ๊ธฐ๋Šฅ์„ ๊ธฐ์ค€์œผ๋กœ ํŒŒ์ผ ๋‹จ์œ„๋กœ ๋ถ„๋ฆฌํ•œ๋‹ค. ์ด๋•Œ ๋ชจ๋“ˆ์ด ์„ฑ๋ฆฝํ•˜๋ ค๋ฉด ๋ชจ๋“ˆ์€ ์ž์‹ ๋งŒ์˜ ํŒŒ์ผ ์Šค์ฝ”ํ”„(๋ชจ๋“ˆ ์Šค์ฝ”ํ”„)๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

์ž์‹ ๋งŒ์˜ ํŒŒ์ผ ์Šค์ฝ”ํ”„๋ฅผ ๊ฐ–๋Š” ๋ชจ๋“ˆ์˜ ์ž์‚ฐ(๋ชจ๋“ˆ์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ณ€์ˆ˜, ํ•จ์ˆ˜, ๊ฐ์ฒด ๋“ฑ)์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋น„๊ณต๊ฐœ ์ƒํƒœ๋‹ค. ๋‹ค์‹œ ๋งํ•ด, ์ž์‹ ๋งŒ์˜ ํŒŒ์ผ์Šค์ฝ”ํ”„๋ฅผ ๊ฐ–๋Š” ๋ชจ๋“ˆ์˜ ๋ชจ๋“  ์ž์‚ฐ์€ ์บก์Аํ™”๋˜์–ด ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค. ์ฆ‰, ๋ชจ๋“ˆ์€ ๊ฐœ๋ณ„์  ์กด์žฌ๋กœ์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜๊ณผ ๋ถ„๋ฆฌ๋˜์–ด ์กด์žฌํ•œ๋‹ค. 

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์ง€์ •ํ•œ ๋ณ€์ˆ˜๋“ค์€ global scope๋กœ ์ง€์ •๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์‹ค์ƒ ํ•˜๋‚˜์˜ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์—์„œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ํšจ๊ณผ๋ฅผ ๊ฐ€์ง„๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ๋™์ผํ•œ ์ด๋ฆ„์˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋ฎ์–ด ์“ฐ๋Š” ํšจ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋œ๋‹ค. 

์ด๋ ‡๊ฒŒ ์ €์žฅ๋œ ๋ณ€์ˆ˜๋“ค์€ window์—์„œ ์กฐํšŒ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

export, importํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

module์—ญ์‹œ class์™€ ๊ฐ™์ด strict mode์—์„œ ์ž‘๋™ํ•œ๋‹ค.

 

globalThis

The global globalThis property contains the global this value, which is akin to the global object.

Search for the global across environments

Usually, the global object does not need to be explicitly specified — its properties are automatically accessible as global variables.

However, one case where one needs to explicitly access the global object is when writing to it, usually for the purpose of polyfills.

Prior to globalThis, the only reliable cross-platform way to get the global object for an environment was Function('return this')(). However, this causes CSP violations in some settings, so authors would use a piecewise definition like this (slightly adapted from the original core-js source):

 

With globalThis available, the additional search for the global across environments is not necessary anymore:

if (typeof globalThis.Intl === 'undefined') {
  Object.defineProperty(globalThis, 'Intl', {
    value: {
      // Our Intl implementation
    },
    enumerable: false,
    configurable: true,
    writable: true,
  });
}

By using globalThis, your code will work in window and non-window contexts without having to write additional checks or tests. In most environments, globalThis directly refers to the global object of that environment. In browsers, however, a proxy is used internally to take into account iframe and cross-window security. In practice, it doesn’t change the way you write your code, though.

Generally, when you’re not sure in what environment your code will be used, or when you want to make your code executable in different environments, the globalThis property comes in very handy. You’ll have to use a polyfill to implement the feature on older browsers that do not support it, however.

On the other hand, if you’re certain what environment your code is going to be used in, then use one of the existing ways of referencing the environment’s global object and save yourself from the need to include a polyfill for globalThis.

 

quiz

์งˆ๋ฌธ 1:

์—ฌ๋Ÿฌ <script src="...">๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ๋ณด๋‹ค JavaScript ๋ชจ๋“ˆ์ด ๋” ๋‚˜์€ ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?

  • ์ฐจ์ด ์—†์Šต๋‹ˆ๋‹ค - ๋‘ ๊ฐ€์ง€ ์ ‘๊ทผ ๋ฐฉ์‹ ์ค‘ ์•„๋ฌด๊ฑฐ๋‚˜ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ๋ชจ๋“ˆ์˜ ์„ฑ๋Šฅ์ด ๋” ์ข‹์Šต๋‹ˆ๋‹ค - ์ด๊ฒƒ์ด ๋ชจ๋“ˆ์„ ์จ์•ผ ํ•˜๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค.

  • ๊ฐ ํŒŒ์ผ์ด ์ข…์†์„ฑ์„ ์ง€์ •ํ•˜๋ฏ€๋กœ ์˜ฌ๋ฐ”๋ฅธ ์ˆœ์„œ๋ฅผ ์ˆ˜๋™์œผ๋กœ ์ผ์ผ์ด ๊ด€๋ฆฌํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

์งˆ๋ฌธ 2:

์•„๋ž˜์˜ export ๋ฌธ์— ๋Œ€ํ•ด ์˜ณ์€ ๊ฒƒ์€ ๋ฌด์—‡์ผ๊นŒ์š”?

  1. export const myName = 'Max';
  • ํŒŒ์ผ์˜ ์œ ์ผํ•œ export์ฒ˜๋Ÿผ ๋ณด์ด๋‹ˆ ์ด๋ฅธ๋ฐ” “๊ธฐ๋ณธ export”์ž…๋‹ˆ๋‹ค.

  • ์ด๊ฒƒ์€ ์†Œ์œ„ "๋ช…๋ช…๋œ export"์ž…๋‹ˆ๋‹ค - myName์ด๋ผ๋Š” ์ด๋ฆ„ ์•„๋ž˜์— ๊ฐ’์ด 'Max'์ธ ์ƒ์ˆ˜๋ฅผ ๋‚ด๋ณด๋‚ด๋Š” ๊ฒ๋‹ˆ๋‹ค.

  • ์œ ํšจํ•˜์ง€ ์•Š์€ ๊ตฌ๋ฌธ์ด ์žˆ๋Š” ์˜ˆ์ž…๋‹ˆ๋‹ค - const ํ‚ค์›Œ๋“œ๋ฅผ ์ œ๊ฑฐํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์งˆ๋ฌธ 3:

๋‹ค์Œ์˜ export๋ฅผ ์–ด๋–ป๊ฒŒ ์ž„ํฌํŠธ ํ•ด์•ผ ํ• ๊นŒ์š”?

export const myName = 'Max';
export function greet() { ... }
    1. import personData from './file.js';
    1. import { myName } from './file.js';
    1. import * as personData from './file.js';

์ด๋Š” ํŒŒ์ผ์˜ ๋ชจ๋“  export๋ฅผ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ ๋ฌถ์Šต๋‹ˆ๋‹ค. ์ด(๋ฌถ๋Š” ๊ธฐ์ˆ )๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๋Œ€์•ˆ์€ ์ž„ํฌํŠธํ•˜๋ ค๋Š” ๋ชจ๋“  ๋ช…๋ช…๋œ export๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ง€์ •ํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค: "import { Greeting, myName } from './file.js'".

 

์งˆ๋ฌธ 4:

๋ชจ๋“ˆ์—์„œ ๋‚ด๋ณด๋‚ด์ง€ ์•Š์€ ์ฝ”๋“œ๋Š” ์–ธ์ œ, ์–ผ๋งˆ๋‚˜ ์ž์ฃผ ์‹คํ–‰๋ ๊นŒ์š”?

  • ๋ชจ๋“ˆ์ด ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ์ž„ํฌํŠธ๋  ๋•Œ ํ•ญ์ƒ.

  • ๋ชจ๋“ˆ์ด ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ ์ฒ˜์Œ์œผ๋กœ ์ž„ํฌํŠธ๋  ๋•Œ.

  • ์ ˆ๋Œ€ ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค

 

 

 

sources

https://www.npmjs.com/package/serve

 

serve

Static file serving and directory listing. Latest version: 14.0.1, last published: 4 months ago. Start using serve in your project by running `npm i serve`. There are 653 other projects in the npm registry using serve.

www.npmjs.com

https://books.google.se/books?id=JQ1sEAAAQBAJ&printsec=frontcover&dq=%EB%AA%A8%EB%8D%98%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8+deep+dive&hl=en&sa=X&redir_esc=y#v=onepage&q=%EB%AA%A8%EB%8D%98%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%20deep%20dive&f=false 

 

๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive

์ด์›…๋ชจ ์ผ๋ณธ์—์„œ ์ปดํ“จํ„ฐ๊ณตํ•™์„ ์ „๊ณตํ•œ ํ›„ ์ผ๋ณธ์˜ ์ž๋™์ฐจ ์—ฐ๊ตฌ์†Œ ๊ณต์šฉ ์›น ํ”„๋ ˆ์ž„์›Œํฌ ๊ฐœ๋ฐœ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„์— ๋ฐœ์„ ๋“ค์—ฌ ๋†“์•˜๋‹ค. ์ดํ›„ ์™ธ๊ตญ๊ณ„ IT ๊ธฐ์—…์—์„œ ์†Œํ”„ํŠธ์›จ์–ด ์ปจ์„ค

books.google.co.jp

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis

 

globalThis - JavaScript | MDN

The global globalThis property contains the global this value, which is akin to the global object.

developer.mozilla.org

https://blog.logrocket.com/what-is-globalthis-why-use-it/

 

What is globalThis, and why should you start using it? - LogRocket Blog

The globalThis proposal, now in stage 4, aims to consolidate the increasingly fragmented ways of accessing the global object in JavaScript.

blog.logrocket.com