ํฐ์คํ ๋ฆฌ ๋ทฐ
[Udemy] JavaScript: The Advanced Concepts - ์น์ 3:Javascript Foundation II
Carrot๐ฅ 2020. 8. 15. 02:27
** โ priv | ๊ฐ์๋
ธํธ
์นดํ
๊ณ ๋ฆฌ์ ์๋ ๋ด์ฉ์ ๋ฐํ์ด ๋ชฉ์ ์ด ์๋๊ธฐ ๋๋ฌธ์ ์ ํ ์ ์ ๋์ด์์ง ์์ต๋๋น.**
์น์ 3:Javascript Foundation II
29. Execution Context
Creation Phase
- argument object created with any arguments
- initializes
this
keyword to point called or to the global object if not specified
Executing Phase
- Variable Environment created - memory space for variable and functions created
- initializes all variables to
undefined
and places them into memory with any new functions
35. Function Invocation
// Function Expression
var canada = () => {
console.log('cold');
}
// Function Declaration
function india() {
console.log('warm');
}
// Function Invocation/Call/Execution
canada();
india();
canada function (Function Expression) is defined at runtime
when we actually run/call/execute/invoke the function.
ใด ํจ์ํํ์์ runtime ๋ ์ ์๋จ.๋ณ์ ํธ์ด์คํ
์ด ์ผ์ด๋๋ฏ๋ก canada
๋ผ๋ ๋ณ์๊ฐ undefined
๋ก ์์ฑ๋์๋ค๊ฐ, ํจ์๊ฐ ์ค์ ๋ก ์คํ๋ ๋ ๋ด์ฉ์ด ๋ณ์์ ๋ด๊น
india function (Function Declaration) ์ ํจ์ ํธ์ด์คํ
์ด ์ผ์ด๋์
ํธ์ด์คํ
๋๋ฉด์ ๋ด์ฉ์ด ๋ฐ๋ก ์ ์๋จ
That is when the compiler initially looks at the code
and starts hoisting and allocating memory.
๊ธ๊ณ ์๋ ์คํ์ปจํ
์คํธ๊ฐ ๋ง๋ค์ด์ง๋ (๊ธ๊ป ํจ์๊ฐ ์คํ๋๋ ์๊ฐ),
this ํค์๋๋ arguments ๋ ๊ฐ์ด ๋ง๋ค์ด์ง
36. arguments Keyword
๊ธ๋ก๋ฒ ์ปจํ
์คํธ๋ง๊ณ ๊ฑ ํจ์ ์คํ๋ ๋ ์ฌ์ฉ ๊ฐ๋ฅ
๊ธ๋ก๋ฒ ์ปจํ
์คํธ์์ ์ฌ์ฉํ๋ฉด ์๋ฌ๋จ (arguments is not defined)
The keyword arguments
can be dangerous to use in your code as is. In ES6, a few methods were introduced that can help better use arguments. => ...arg
์ array๊ฐ ์๋๋ผ array like object์
๊ทธ๋์ array ๋ฉ์๋๋ชป์
var args = Array.from(arguments);
var args = [...arguments]; // ํผ์น๊ธฐ ์ฐ์ฐ์, ์ ๊ฐ ๊ตฌ๋ฌธ
Rest Parameters
function f(a, b, ...theArgs) {
// ...
}
-
ํจ์์ ๋ง์ง๋ง ํ๋ผ๋ฏธํฐ์ ์์
...
๋ฅผ ๋ถ์ฌ (์ฌ์ฉ์๊ฐ ์ ๊ณตํ) ๋ชจ๋ ๋๋จธ์ง ์ธ์๋ฅผ "ํ์ค" ์๋ฐ์คํฌ๋ฆฝํธ ๋ฐฐ์ด๋ก ๋์ฒดํฉ๋๋ค. ๋ง์ง๋ง ํ๋ผ๋ฏธํฐ๋ง "Rest ํ๋ผ๋ฏธํฐ" ๊ฐ ๋ ์ ์์ต๋๋ค. -
rest ํ๋ผ๋ฏธํฐ๋
Array
์ธ์คํด์ค๋ก,sort
,map
,forEach
๋๋pop
๊ฐ์ ๋ฉ์๋๊ฐ ๋ฐ๋ก ์ธ์คํด์ค์ ์ ์ฉ๋ ์ ์์์ ๋ปํฉ๋๋ค. -
๋จ, Restํ๋ผ๋ฏธํฐ๋ ํญ์ ์ ์ผ ๋ง์ง๋ง ํ๋ผ๋ฏธํฐ๋ก ์์ด์ผ ํ๋ค.
-
ES6 ์ arrow function ์์๋
arguments
ํ๋กํผํฐ๊ฐ ์์ ์์
๋ฐ๋ผ์ ํ์ดํ ํจ์๋ก ๊ฐ๋ณ ์ธ์ ํจ์๋ฅผ ๊ตฌํํด์ผ ํ ๋๋ ๋ฐ๋์ rest ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
โ ++ spread ์ฐ์ฐ์
function sandwich(a, b, c) {
console.log(a); // '๐'
console.log(b); // '๐ฅฌ'
console.log(c); // '๐ฅ'
}
const food = ['๐', '๐ฅฌ', '๐ฅ'];
// Old way
sandwich.apply(null, food);
// โ
ES6 way
sandwich(...food);
์์์ ์ดํด๋ณธ Rest ํ๋ผ๋ฏธํฐ๋ Spread ๋ฌธ๋ฒ์ ์ฌ์ฉํ์ฌ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ์ํ ๊ฒ์ ์๋ฏธํ๋ค. ํํ๊ฐ ๋์ผํ์ฌ ํผ๋ํ ์ ์์ผ๋ฏ๋ก ์ฃผ์๊ฐ ํ์ํ๋ค.
// spread ๋ฌธ๋ฒ์ ์ํด ๋ถ๋ฆฌ๋ ๋ฐฐ์ด์ ์์๋ ๊ฐ๋ณ์ ์ธ ์ธ์๋ก์ ๊ฐ๊ฐ์ ๋งค๊ฐ๋ณ์์ ์ ๋ฌ๋๋ค.
foo(1, ...[2, 3], 4, ...[5]);
https://poiemaweb.com/es6-extended-parameter-handling
37. Variable Environment
function one() {
var isValid = true; // local env
console.log("one: ", isValid); // true
two(); // new execution context!
}
function two() {
var isValid; // undefined
console.log("two: ", isValid); // undefined
}
var isValid = false; // global
console.log("๊ธ๋ก๋ฒ: ", isValid); // false
one();
/*
two() isValid = undefined
one() isValid = true
global() isValid = false
------------------------
CALL STACK
*/
38. Scope Chain
39. [[scope]]
The execution context tells the engine
which lexical environment it is currently working in
and the lexical scope determines the available variables.
์ค์ฝํ์ฒด์ธ์ ํตํด ๋ถ๋ชจenv์ ๋ณ์์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
Lexical Environment === [[scope]]
[[scopes]]
ํ๋กํผํฐ๋ฅผ ํตํด์ ์ค์ฝํ์ฒด์ธํ์ธ ๊ฐ๋ฅ
40. Exercise: JS is Weird
function weird() {
height = 50; // ๐ค
return height;
}
weird();
2๋ฒ์งธ๐ค์ค์์ ์ผ์ด๋๋์ผ
weird :ใ
ใ
ใ
? our variable env์๋ height
๊ฐ ์์ด..
๊ทธ๋ฌ๊ณ ๊ธ๋ก๋ฒ env๋ก ๊ฐ์ ๋ฌผ์ด๋ด
๊ธ๋ก๋ฒ : ใ
ใ
ใ
? ๋๋ ์์ด ๊ทผ๋ฐ ํ๋ํ์ํ๋ค๋๊น ํ๋๋ง๋ค์ด๋๋ฆผ
๊ทธ๋๊น weird ํจ์์ variable env ์ ์ ์ฅ๋๋๊ฒ ์๋..
We call this leakage of global variables.
์ ์ฝ๋๋ฅผ ์ค์ ๋ก ์คํํด๋ณด๋ฉดwindow.height
๋ผ๊ณ ์ ์ญ๊ฐ์ฒด์ ์์ฑ๋์ด์๋ฌ...!
์๊ฑฐ ๋ฐฉ์งํ๋ ค๋ฉด ‘use strict’
์ ์ฐ๋ฉด ๋จ
๊ณ ๋ฌ๋ฉด height is not defined ๋ผ๊ณ ๋ ํผ๋ฐ์ค์๋ฌ๊ฐ ๋จ
-
ํจ์ํํ์์์ ์ต๋ช ํจ์๊ฐ ์๋๋ผ ๊ธฐ๋ช ํจ์๋ฅผ ์ฐ๋ฉด
var hey = function doodle(){ // doodle(); return "HEY"; } hey(); doodle();
doodle์์์ doodle์ด๋ผ๋ ์ด๋ฆ์ผ๋ก ์๊ธฐ์์ (ํจ์)์๋ ์ ๊ทผ ๊ฐ๋ฅํ์ง๋ง
์ธ๋ถ์์๋ doodle๋ก ์ ๊ทผ ๋ชปํจ๊ธ๊ป
doodle
์ด๋ผ๋ ์ด๋ฆ์ ์๊ธฐ์์ ์ ์ค์ฝํ์ enclosed ๋์ด์์hey
๋ ๊ธ๋ก๋ฒ ์ปจํ ์คํธ์ ์๊ธฐ๋์
๊ธ๋ก๋ฒ์์๋ hey๋ก ๋ถ๋ฅผ ์ ์๊ณ
๋น๊ทผ doodle ์์์๋ hey๋ก ๋ถ๋ฅผ ์ ์์
43. Global Variables
<script>var x = 1;</script>
<script>var y = 2;</script>
<script>var z = 3;</script>
<script>var x = 5000;</script>
์ ์ญ๋ณ์๋ฅผ ์ง์ํด์ผํจ
๋ค๋ฅธ ํ์ผ์์๋ ์ ์ํ๋ฉด ์์ ์ ์ญ๋ณ์x๋ฅผ overwriteํ๊ฒ๋จ
Avoid polluting the global namespace or scope when possible.
44. IIFE
IIFE - Immediately Invoked Function Expression ์ฆ์์คํํจ์
๋ก์ปฌ์ค์ฝํ์์ ๊ฐ๋๊ธฐ์ํด ์ฌ์ฉ
var script1 = (function () {
function foo() { return "Hi"; }
return { foo };
})();
script1.foo(); // Hi
์ด๋ฐ์์ผ๋ก๋ ์ฌ์ฉ ๊ฐ๋ฅ
์ถ์ฒ : Udemy - JavaScript: The Advanced Concept
https://www.udemy.com/course/advanced-javascript-concepts
'๊ณต๋ถ > priv | ๊ฐ์๋ ธํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
- VSC
- eventlistener
- GIT
- Stash
- ๋ถํธ์บ ํ
- string
- ์ฝ๋ฉ๋ถํธ์บ ํ
- KEYBOARD
- js
- HTML
- ๋ฐ๋๋ผ์ฝ๋ฉ ํ๊ธฐ
- ๋ฐ๋๋ผ์ฝ๋ฉ
- book
- eslint
- ์์ฑ์ํจ์
- review
- stackoverflow
- array
- DOM
- css
- Total
- Today
- Yesterday