JavaScript - The Complete Guide 2022

Section 5: Behind The Scenes of JavaScript - How It Works, The Weird Parts & ES5 vs ES6+

olivia_yj 2022. 10. 14. 02:10

The goals

๐Ÿ’ช๐ŸปES5 vs ES6+ Syntax

โœŒ๐ŸปHow JavaScript Works

๐Ÿ‘๐ŸปThe Weird Parts

 

 

ES5 vs ES6+

This is mostly important for browser vendors who need to make sure that their JavaScript engine follows this ECMAScript definition

 

Var vs Let vs Const

Hoisting

JavaScript has a special feature called hoisting which in the end means that the JavaScript engine, the browser when it loads our script, it goes all over our script and look for functions which it then automatically loads and registers so that we can write functions below the code where we actually use them.

That's something I covered earlier in the course, it does the same for variables created with var, it actually pulls this variable declaration to the beginning of our file.

 

Strict Mode in JavaScript

JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode". Strict mode isn't just a subset: it intentionally has different semantics from normal code. Browsers not supporting strict mode will run strict mode code with different behavior from browsers that do, so don't rely on strict mode without feature-testing for support for the relevant aspects of strict mode. Strict mode code and non-strict mode code can coexist, so scripts can opt into strict mode incrementally.

Strict mode makes several changes to normal JavaScript semantics:

  1. Eliminates some JavaScript silent errors by changing them to throw errors.
  2. Fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that's not strict mode.
  3. Prohibits some syntax likely to be defined in future versions of ECMAScript.

Invoking strict mode

Strict mode applies to entire scripts or to individual functions. It doesn't apply to block statements enclosed in {} braces; attempting to apply it to such contexts does nothing. eval code, Function code, event handler attributes, strings passed to setTimeout(), and related functions are entire scripts, and invoking strict mode in them works as expected.

Strict mode for scripts

To invoke strict mode for an entire script, put the exact statement "use strict"; (or 'use strict';) before any other statements.

// Whole-script strict mode syntax
'use strict';
const v = "Hi! I'm a strict mode script!";
 
 
Strict mode for functions

Likewise, to invoke strict mode for a function, put the exact statement "use strict"; (or 'use strict';) in the function's body before any other statements.

function myStrictFunction() {
  // Function-level strict mode syntax
  'use strict';
  function nested() {
    return 'And so am I!';
  }
  return `Hi! I'm a strict mode function! ${nested()}`;
}
function myNotStrictFunction() {
  return "I'm not strict.";
}

In strict mode, starting with ES2015, functions inside blocks are scoped to that block. Prior to ES2015, block-level functions were forbidden in strict mode.

 

Quiz

์•„๋ž˜์˜ ์ฝ”๋“œ๋Š” ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž‘๋™ํ• ๊นŒ์š”?

console.log(name);
const name = 'Max';

์ •๋‹ต์€ '์•„๋‹™๋‹ˆ๋‹ค'

const๊ณผ let์€ ํ˜ธ์ด์ŠคํŒ…์ด var์ฒ˜๋Ÿผ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค(์ด ์˜ˆ์ œ์—์„œ๋Š” var๊ณผ ํ•จ๊ป˜ ์ž‘๋™ํ–ˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค).

 

 

[ํŒŒํ—ค์น˜๊ธฐ] JavaScript ์–ธ์–ด vs ๋ธŒ๋ผ์šฐ์ € API

์ง€๋‚œ ๊ฐ•์˜์—์„œ๋Š” JavaScript ์—”์ง„๊ณผ ์—”์ง„์ด ๋ธŒ๋ผ์šฐ์ € ๋‚ด๋ถ€์—์„œ ํ•˜๋Š” ์ผ์„ ๋‹ค๋ค„ ๋ดค์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ JS ์ฝ”๋“œ ์‹คํ–‰๊ณผ ํ•ด๋‹น ์‹คํ–‰ ์ค‘์— ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ธŒ๋ผ์šฐ์ € API ๊ฐ„์— ์ฐจ์ด๊ฐ€ ์žˆ์Œ์„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค.

์ž‘์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1) JavaScript ์–ธ์–ด
ํ•ต์‹ฌ ๊ตฌ๋ฌธ(let, const ๋“ฑ)์€ ์ดํ•ดํ•˜์ง€๋งŒ ์˜ˆ๋ฅผ ๋“ค์–ด DOM์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ฌด๊ฒƒ๋„ ๋ชจ๋ฆ…๋‹ˆ๋‹ค.

2) Browser API
์ฝ”๋“œ๋ฅผ ์ดํ•ดํ•  ์ฑ…์ž„์ด ์—†๋Š”(์ด๋Š” 1)์ด ํ•˜๋Š” ์—ญํ• ์ž…๋‹ˆ๋‹ค) ๋Œ€์‹  ์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” DOM API์™€ ๊ฐ™์€ API๋ฅผ ๋…ธ์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

JavaScript ์–ธ์–ด(1)๋Š” Ecma ์กฐ์ง์˜ ์ผ๋ถ€์ธ ๊ทธ๋ฃน์ธ Ecma International Technical Committee 39(TC39)์— ์˜ํ•ด ๋ฐœ์ „๋˜์—ˆ์Šต๋‹ˆ๋‹ค. JavaScript ์–ธ์–ด ์ž์ฒด์— ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๋Š” ์ผ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๊ณผ๊ฑฐ์—๋Š” let๋ฐ const๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์—ญํ• ์„ ํ•˜๊ธฐ๋„ ํ–ˆ์ฃ .

TC39์— ๋Œ€ํ•ด์„œ๋Š” ์ด ๋งํฌ์—์„œ ์ž์„ธํžˆ ์•Œ์•„๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. https://tc39.es/

๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๊ทธ๋ฃน์—์„œ ๋…ผ์˜ ์ค‘์ธ ํ˜„์žฌ ์ œ์•ˆ, ์ฆ‰ ํ–ฅํ›„ ํ•ต์‹ฌ JavaScript ์–ธ์–ด๋กœ ๋งŒ๋“ค ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ๊ธฐ๋Šฅ์„ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. https://github.com/tc39/proposals

์ค‘์š”ํ•œ ์ฐธ๊ณ  ์‚ฌํ•ญ. ํ•œ ๊ธฐ๋Šฅ์ด ์–ธ์–ด์— ํฌํ•จ๋œ๋‹ค๊ณ  ํ•ด์„œ ๋ชจ๋“  JavaScript ์—”์ง„์ด ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์ฆ‰์‹œ ์ง€์›ํ•œ๋‹ค๋Š” ์˜๋ฏธ๋Š” ์•„๋‹™๋‹ˆ๋‹ค . ๋ฌผ๋ก  ์—”์ง„์„ ๊ฐœ๋ฐœํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์€ ๊ฐ€๋Šฅํ•œ ํ•œ ๋นจ๋ฆฌ ์ง€์›์„ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด ์ตœ์„ ์„ ๋‹คํ•˜๊ฒ ์ง€๋งŒ ๊ทธ ๊ณผ์ •์—๋„ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค.

๋ฐ˜๋ฉด์— ์—”์ง„ ๊ณต๊ธ‰์—…์ฒด๋Š” ๋•Œ๋กœ๋Š” TC39๊ฐ€ ๊ธฐ๋Šฅ์„ JavaScript์˜ ๊ณต์‹ ๋ถ€๋ถ„์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์ „์— ํŠน์ • ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๊ธฐ ์‹œ์ž‘ํ•˜๊ธฐ๋„ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ๊ตญ JS ์—”์ง„์ด ์ดํ•ดํ•˜๋Š” ๊ตฌ๋ฌธ์„ ๊ฒฐ์ •ํ•˜๋Š” ๊ฒƒ์€ ์ „์ ์œผ๋กœ ์—”์ง„์„ ์ž‘์—…ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์—๊ฒŒ ๋‹ฌ๋ ค ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

๋ธŒ๋ผ์šฐ์ € API ๋˜ํ•œ ๋‹ค์–‘ํ•œ ๋ธŒ๋ผ์šฐ์ € ๊ณต๊ธ‰์—…์ฒด(Chrome์šฉ Google, Edge์šฉ Microsoft ๋“ฑ)๊ฐ€ ๊ธฐ๋Šฅ ํŒจ๋ฆฌํ‹ฐ์™€ (๋Œ€๋žต) ์œ ์‚ฌํ•œ API๋ฅผ ์ œ๊ณตํ•˜๊ณ ์ž ํ‘œ์ค€ํ™”๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋‹ค๋ฅธ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ž‘๋™ํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค๋ฅธ ๊ธฐ๋Šฅ์„ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค๋ฉด ํ›Œ๋ฅญํ•œ ๊ฐœ๋ฐœ์ž ๊ฒฝํ—˜์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์—†๊ฒ ์ฃ . ํ•˜์ง€๋งŒ ๊ณผ๊ฑฐ์—๋Š” ์ด๋Ÿฐ ๊ฒฝ์šฐ๊ฐ€ ๋Œ€๋ถ€๋ถ„์ด์—ˆ์Šต๋‹ˆ๋‹ค.

 

ํ˜„์žฌ ๊ฐ์‚ฌํ•˜๊ฒŒ๋„ ๋ธŒ๋ผ์šฐ์ € API๋ฅผ ๋‹ค๋ฃจ๋Š” ์‹ค๋ฌด๋‹จ๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ฐฉ์‹์ด ์ ์  ๋” ๊ฐœ์„ ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ€๋Šฅํ•œ ํ•œ ์„œ๋กœ ๋‹ค๋ฅธ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์„œ๋กœ ๋‹ค๋ฅธ ๊ธฐ๋Šฅ๊ณผ API๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋˜๋Š” ํ˜„์ƒ์„ ์ž˜ ํ”ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์‹ค๋ฌด๋‹จ์˜ ์ด๋ฆ„์€ WHATWG ์ด๋ฉฐ ์ž์„ธํ•œ ๋‚ด์šฉ์€ https://whatwg.org/์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ์‹ค๋ฌด๋‹จ์ด ๊ด€๋ฆฌํ–ˆ๋˜/๊ด€๋ฆฌํ•˜๋Š” API์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด๋ ค๋ฉด ๋‹ค์Œ ์‚ฌ์ดํŠธ๋ฅผ ํ™•์ธํ•˜์„ธ์š”. https://spec.whatwg.org/

์ด ์‹ค๋ฌด๋‹จ์€ TC39์™€ ๊ด€๋ จ์ด ์—†์Šต๋‹ˆ๋‹ค!

 

Primitive vs Reference Values

const person1 = {age: 30};
const person2 = {age: 30};
person1 === person2 // false
const hobbies = ['Sports'];
hobbies.push('Cooking');
hobbies  // ["sports", "Cooking"]
// Because they just remember the address where data is saved

hobbies = ['Sports', 'Running']
// Uncaught TypeError: Assignment to constant variable

 

Garbage Collection

์œ ์šฉํ•œ ์ฐธ๊ณ  ์ž๋ฃŒ & ๋งํฌ

๋‹ค์Œ ์ž๋ฃŒ๋“ค์ด ๋„์›€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค - ๊ทธ ์ค‘ ์ผ๋ถ€๋Š” ๋งค์šฐ ์‹ฌํ™”๋œ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋‹ค๋Š” ์ ์— ์œ ์˜ํ•ด ์ฃผ์„ธ์š”.

 

 

 

 

Sources

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#changes_in_strict_mode

 

Strict mode - JavaScript | MDN

JavaScript's strict mode is a way to opt in to a restricted variant of JavaScript, thereby implicitly opting-out of "sloppy mode". Strict mode isn't just a subset: it intentionally has different semantics from normal code. Browsers not supporting strict mo

developer.mozilla.org