μμ μλ£νκ³Ό μ°Έμ‘° μλ£ν
βοΈ μμ μλ£ν
μμ μλ£νμ΄λ? κ°μ²΄κ° μλλ©΄μ methodλ₯Ό κ°μ§μ§ μλ μλ£νμΌλ‘ λ³μμ νλμ λ°μ΄ν°λ§ μ μ₯ν μ μκ³ ν΄λΉ λ°μ΄ν°λ κ° μ체μ΄λ€.
μμ μλ£ν μ’ λ₯ 6κ°μ§string
,number
,bigint
,boolean
,undefined
,symbol
,(null)
"hello world!"
"hello september!"
// "hello world"μ "hello september!"μ λͺ¨λ λ³κ²½ν μ μλ κ³ μ λ κ°μ΄λ€.
let word = "hello world!"
word = "hello september!"
// wordλΌλ λ³μμ μ¬ν λΉμ νμ¬ λ³μμ λ΄κΈ΄ λ΄μ©μ λ³κ²½νλ κ²μ κ°λ₯νλ€.
βοΈ μ°Έμ‘° μλ£ν
μ°Έμ‘° μλ£νμ΄λ? μμ μλ£νμ΄ μλ λͺ¨λ κ²μΌλ‘, μ¬λ¬ κ°μ λ°μ΄ν°λ₯Ό λ΄μ μ μλ€. λ³μμ λ°μ΄ν°μ μ£Όμκ° λ΄κΈ°κ³ λ°μ΄ν°λ heapμ΄λΌλ λ³λμ 곡κ°μ μ μ₯λλ€.
μ°Έμ‘° μλ£ν μ’ λ₯λ°°μ΄([])
,κ°μ²΄({})
,ν¨μ(function () {})
μ€μ½ν
μ€μ½νλ? νμ¬ μ€νλλ 컨ν μ€νΈλ‘, λ³μμ μ κ·Όν μ μλ λ²μλ₯Ό λ§νλ€.
βοΈ μ€μ½νμ νΉμ§
λ°κΉ₯ μ€μ½νμμ μ μΈν λ³μλ μ μ€μ½νμμ μ¬μ© κ°λ₯νκ³ μ μ€μ½νμμ μ μΈν λ³μλ λ°κΉ₯ μ€μ½νμμ μ¬μ© λΆκ°λ₯νλ€.
1. μμμ λ°κΉ₯ μ€μ½νλ‘ μ κ·Όμ΄ κ°λ₯νλ λ°λλ λΆκ°λ₯νλ€.
2. μ€μ½νλ μ€μ²©μ΄ κ°λ₯νλ€.
3. κ°μ₯ λ°κΉ₯ μ€μ½νλ μ μ μ€μ½ν(Global Scope), κ·Έ μΈλ μ§μ μ€μ½ν(Local Scope)λΌκ³ νλ€.
4. μ§μ μ€μ½νμ μ μΈν λ³μλ μ§μ λ³μ, μ μ μ€μ½νμ μ μΈν λ³μλ μ μ λ³μλΌ νλλ° μ§μλ³μλ μ μ λ³μλ³΄λ€ λμ μ°μ μμλ₯Ό κ°μ§λ€.
let username = 'sonsubin';
if (username) {
let message = `Hello, ${username}!`;
console.log(message); // 'Hello, sonsubin!'
}
console.log(message); // Uncaught ReferenceError: message is not defined
// messageλΌλ λ³μκ° μ μ€μ½νμ μ μΈλμ΄ μμΌλ―λ‘ λ°κΉ₯ μ€μ½νμμλ μ κ·Όν μ μλ€.
let name = 'μμλΉ'; // μ μ λ³μ
function showName() {
let name = 'νκΈΈλ'; // μ§μ λ³μ
console.log(name);
}
console.log(name); // 'μμλΉ'
showName(); // 'νκΈΈλ'
console.log(name); // 'μμλΉ'
// 9λ²μ§Έ μ€μ μ§μ λ³μκ° μ μ λ³μλ³΄λ€ μ°μ μμκ° λκΈ° λλ¬Έμ 'μμλΉ'λ³΄λ€ 'νκΈΈλ'μ΄ λ¨Όμ μΆλ ₯λλ€.
βοΈ μ€μ½νμ μ’ λ₯
- λΈλ‘ μ€μ½ν(Block Scpoe) : μ€κ΄νΈμ νμ΄νλ‘ λλ¬μΌ λ²μμ΄λ€. νμ΄νλ‘ λλ¬μΌ λ²μλ λΈλ‘ μ€μ½νμ΄λ―λ‘ νμ΄ν ν¨μλ ν¨μ μ€μ½νκ° μλ λΈλ‘ μ€μ½νμ΄λ€.
- ν¨μ μ€μ½ν(Function Scope) : ν¨μ(function)λ‘ λλ¬μΌ λ²μμ΄λ€.
λ³μ μ μΈμ λ°λ₯Έ μ€μ½ν μ ν¨ λ²μ
let | const | var | |
μ ν¨ λ²μ | λΈλ‘ μ€μ½ν λ° ν¨μ μ€μ½ν | λΈλ‘ μ€μ½ν λ° ν¨μ μ€μ½ν | ν¨μ μ€μ½ν |
κ°μ μ¬ν λΉ | O | X | O |
λ³μμ μ¬μ μΈ | X | X | O |
μ μΈ μλ λ³μ ν λΉμ λ°©μ§νκΈ° μν΄ Sctric Modeλ₯Ό μ μ©νλ©΄ λ³μμ μ μΈμ΄ μμ΄ κ°μ ν λΉνμ λ μλ¬λ‘ νλ¨νλ€.
Strict Modeλ₯Ό μ μ©νκΈ° μν΄μλ js νμΌ μλ¨μ'use strict';
λ₯Ό μ λ ₯νλ©΄ λλ€.
ν΄λ‘μ
ν΄λ‘μ λ? ν¨μμ ν¨μκ° μ μΈλ μ΄νμ νκ²½μ μ‘°ν©μΌλ‘ μΈλΆ ν¨μμ λ³μκ° μ μΈλμ΄ μκ³ , μ΄ λ³μμ μ κ·Όν μ μλ λ΄λΆ ν¨μλ₯Ό ν΄λ‘μ λΌκ³ νλ€.
// const adder = x => y => x + y;
const adder = function(x) {
return function(y) {
return x + y;
}
}
βοΈ ν΄λ‘μ μ νΉμ§
- μΈλΆ ν¨μμ 리ν΄κ°μ΄ ν¨μμ ννμ΄λ€.
- λ΄λΆ ν¨μλ μΈλΆ ν¨μμ μ μΈλ λ³μμ μ κ·Όμ΄ κ°λ₯νλ€.
// const adder = x => y => x + y;
const adder = function(x) {
return function(y) {
return x + y;
}
}
typeof adder(4); // 'function' : 리ν΄κ°μ΄ ν¨μμ΄λ€.
adder(4); // y = 4 + y : λ΄λΆ ν¨μλ μΈλΆ ν¨μμ μ μΈλ λ³μμ μ κ·Όμ΄ κ°λ₯νλ€.
βοΈ ν΄λ‘μ μ νμ©
- λ°μ΄ν°λ₯Ό 보쑴νλ ν¨μ : μΈλΆ ν¨μμ μ€νμ΄ λλλ μΈλΆ ν¨μμ λ³μκ° λ©λͺ¨λ¦¬ μμ μ μ₯λλ€.
// const adder = x => y => x + y;
const adder = function(x) {
return function(y) {
return x + y;
}
}
const add4 = adder(4);
add4(8); // 12
add4(12); // 16
// ν¨μμ μ€νμ΄ λλλ 4λΌλ κ°μ΄ μ μ₯λΌμμΌλ―λ‘ 4+8=12μ 4+12=16μ΄ κ²°κ³Όκ°μΌλ‘ μΆλ ₯λλ€.
// HTML λ¬Έμμ΄ μμ±κΈ°
const tagMaker = tag => content => `<${tag}>${content}</${tag}>`;
const divMaker = tagMaker('div');
divMaker('hello'); // '<div>hello</div>'
divMaker('kakao'); // '<div>kakao</div>'
const anchorMaker = tagMaker('a');
anchorMaker('go'); // '<a>go</a>'
anchorMaker('tistory'); // '<a>tistory</a>'
- μ 보μ μ κ·Ό μ ν(μΊ‘μν) : μ μΈν ν¨μλ₯Ό λ°κΎΈμ§ μκ³ ν¨μ λ΄λΆμ λ³μμ μ κ°μ ν λΉν μ μλ€.
const makeCounter = () => {
let value = 0;
return {
increase: () => {
value = value + 1
},
decrease: () => {
value = value - 1
},
getValue: () => value
}
}
const counter1 = makeCounter(); // makeCounter ν¨μλ₯Ό λ³κ²½νμ§ μκ³ valueμ μ κ°μ ν λΉν μ μλ€.
counter1; // { increase: f, decrease: f, getValue: f }
- λͺ¨λν : ν¨μμ μ¬μ¬μ©μ±μ κ·Ήλννμ¬ ν¨μ νλλ₯Ό μμ ν λ 립μ μΈ λΆν ννλ‘ λΆλ¦¬νλ€.
const counter1 = makeCounter();
counter1.increase();
counter1.increase();
counter1.decrease();
counter1.getValue(); // 1
const counter2 = makerCounter();
counter2.decrease();
counter2.decrease();
counter2.decrease();
counter2.getValue(); // -3
// counter1κ³Ό counter2λ λ
립μ μΌλ‘ μλνκΈ° λλ¬Έμ κ°κ° μ΄κΈ° valueμ κ°μ 0μΌλ‘ κ°μ§λ€.
ES6 μ£Όμ λ¬Έλ²
βοΈ spread/rest λ¬Έλ²
- spread λ¬Έλ² : μ£Όλ‘ λ°°μ΄μ νμ΄μ μΈμλ‘ μ λ¬νκ±°λ, κ°κ°μ μμλ‘ λ£μ λ μ¬μ©νλ€.
function sum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
sum(...numbers); // 6
// numbers λ°°μ΄μ μλ κ°μ λͺ¨λ νμ΄μ μΈμλ‘ μ λ¬νμ¬ 1 + 2 + 3μ΄ μ±λ¦½
- rest λ¬Έλ² : νλΌλ―Έν°λ₯Ό λ°°μ΄μ ννλ‘ λ°μμ μ¬μ©νλ€. νλΌλ―Έν°μ κ°μκ° μ λμ μΌ λ μ μ©νλ€.
function add(...rest) {
let sum = 0;
for (let i of rest) {
sum += i;
}
return sum;
}
add(1, 2, 3); // 6
// 1, 2, 3μ λ°°μ΄λ‘ λ§λ€μ΄ forλ¬Έμ μ λ¬νλ―λ‘ 1 + 2 + 3μ΄ μ±λ¦½
spread λ¬Έλ²μ΄ κΈ°μ‘΄μ μλ λ°°μ΄μ νΌμ³ μ¬μ©νλ€λ©΄ rest λ¬Έλ²μ λ°°μ΄μ΄ μλ κ²μ λ°°μ΄λ‘ λ§λ€μ΄μ€λ€.
βοΈ spread/rest λ¬Έλ² νμ©
- λ°°μ΄ ν©μΉκΈ°
let parts = ['shoulders', 'knee'];
let lyrics = ['head', ...parts, 'and', 'toes'];
console.log(lyrics); // [ 'head', 'shoulders', 'knee', 'and', 'toes' ]
let arr1 = [0, 1, 2];
let arr2 = [3, 4, 5];
arr1 = [...arr1, ...arr2]; // spread λ¬Έλ²μ immutableνλ€.
console.log(arr1); // [ 0, 1, 2, 3, 4, 5 ]
- λ°°μ΄ λ³΅μ¬
let arr1 = [1, 2, 3];
let arr2 = [...arr1];
arr2.push(4);
console.log(arr1); // [ 1, 2, 3 ]
console.log(arr2); // [ 1, 2, 3, 4 ]
- κ°μ²΄μμ μ¬μ©
let obj1 = {foo: 'bar', x: 42};
let obj2 = {foo: 'baz', y: 13};
let clonedObj = {...obj1};
let mergedObj = {...obj1, ...obj2};
console.log(clonedObj); // { foo: 'bar', x: 42 }
console.log(mergedObj); // { foo: 'baz', x: 42, y: 13 }
- ν¨μμμ νλΌλ―Έν° λ°μμ€κΈ°
function myFun(a, b, ...manyMoreArgs) {
console.log("a ", a);
console.log("b ", b);
console.log("manyMoreArgs ", manyMoreArgs);
}
myFun("one", "two", "three", "four", "five", "six");
// a one
// b two
// manyMoreArgs [ 'three', 'four', 'five', 'six' ]
'πΆ FE 41κΈ° > Review' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[S2U1] JavaScript κ³ μ°¨ ν¨μ (0) | 2022.09.20 |
---|---|
[S1U11] JS/λΈλΌμ°μ DOM (0) | 2022.09.14 |
[S1U9] JavaScript λ°°μ΄, κ°μ²΄ (0) | 2022.09.05 |
[S1U8] Linux/Git κΈ°μ΄ (0) | 2022.09.01 |
[U1S6] HTML/CSS νμ© (0) | 2022.08.29 |