Section 5: Behind The Scenes of JavaScript - How It Works, The Weird Parts & ES5 vs ES6+
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:
- Eliminates some JavaScript silent errors by changing them to throw errors.
- 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.
- 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!";
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 ์์ง๊ณผ ์์ง์ด ๋ธ๋ผ์ฐ์ ๋ด๋ถ์์ ํ๋ ์ผ์ ๋ค๋ค ๋ดค์ต๋๋ค. ๋ํ 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

์ ์ฉํ ์ฐธ๊ณ ์๋ฃ & ๋งํฌ
๋ค์ ์๋ฃ๋ค์ด ๋์์ด ๋ ์ ์์ต๋๋ค - ๊ทธ ์ค ์ผ๋ถ๋ ๋งค์ฐ ์ฌํ๋ ๋ด์ฉ์ ๋ด๊ณ ์๋ค๋ ์ ์ ์ ์ํด ์ฃผ์ธ์.
- JavaScript ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ์ ๋ํ ์ถ๊ฐ ์ ๋ณด (๊ฐ๋น์ง ์ปฌ๋ ์ ): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management
- V8์ ๊ฐ๋น์ง ์ปฌ๋ ์ ๋ ผ๋ฆฌ: https://v8.dev/blog/free-garbage-collection
- V8์ JavaScript ์์ง์ ์ธ๋ถ์ฌํญ: https://hackernoon.com/javascript-v8-engine-explained-3f940148d4ef
- ์์ ๊ฐ vs ์ฐธ์กฐ ๊ฐ์ ๋ํ ์ถ๊ฐ ์ ๋ณด: https://academind.com/learn/javascript/reference-vs-primitive-values/
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