์์์๋ฃํ
"hello world!"
"hello codestates!"
// "hello world!" ์ "hello codestates!"๋ ๋ชจ๋ ๋ณ๊ฒฝํ ์ ์๋ ๊ณ ์ ๋ ๊ฐ์
๋๋ค.
let word = "hello world!"
word = "hello codestates!"
// ํ์ง๋ง, word๋ผ๋ ๋ณ์์ ์ฌํ ๋น์ ํ์ฌ ๋ณ์์ ๋ด๊ธด ๋ด์ฉ์ ๋ณ๊ฒฝํ๋ ๊ฒ์ ๊ฐ๋ฅํฉ๋๋ค.
const num1 = 123;
num1 = 123456789; // ์๋ฌ ๋ฐ์
// const ํค์๋๋ก ์ ์ธํ๋ฉด, ์ฌํ ๋น์ ๋ถ๊ฐํฉ๋๋ค.
- ๋ณ์์๋ ํ๋์ ๋ฐ์ดํฐ๋ง ๋ด๋๋ค
- string, number, bigint, boolean, undefined, symbol, (null)
let a = 1;
a = b;
b = 2;
a; // ==> 2
์ฐธ์กฐ์๋ฃํ
- ํฌ๊ธฐ๊ฐ ๋์ ์ผ๋ก ๋ณํ๋ ๋ฐ์ดํฐ ๋ณด๊ดํจ
- ์์์๋ฃํ์ด ํ ๋น๋ ๋์๋ ๋ณ์์ ๊ฐ(value)์์ฒด๊ฐ ๋ด๊ธฐ๊ณ , ์ฐธ์กฐ ์๋ฃํ์ด ํ ๋น๋ ๋์๋ ๋ณด๊ดํจ์ ์ฃผ์(reference)๊ฐ ๋ด๊น.
- ๊ทธ๋์ ์ฐธ์กฐ์๋ฃํ์ ๊ณ ์ ๋ ํฌ๊ธฐ์ ๋ณด๊ดํจ์ด ์๋๋ผ ๋์ ์ผ๋ก ํฌ๊ธฐ๊ฐ ๋ณํ๋ ํน๋ณํ ๋ณด๊ดํจ ์ฌ์ฉ
- array, object, function
let e = [10, 20];
lef f = e;
f[0] = 50;
e; // ==> [50, 20]
์ข ํฉํด์ฆ
3. ๋ค์ ์ฝ๋๊ฐ ์คํ๋ ํ, ์ฝ์์ ์ฐํ๋ ๊ฐ์ ๊ฐ๊ฐ ๋ฌด์์ธ์ง ๊ณ ๋ฅด์ธ์.
console.log('codestates' === 'codestates');
console.log(3.14 === 3.14);
console.log([1,2,3] === [1,2,3]);
console.log({ foo: 'bar' } === { foo: 'bar' });
๋ต : true true false false
์๋ฐ์คํฌ๋ฆฝํธ๋ ๋จธ๋ฆฌ๋ฅผ ์๋๋ค. ๊ทธ๋ฌ๋ฉด ๋ฉ๋ชจ๋ฆฌ์ ๊ณต๊ฐ์ ๋ด๊ฐ ์์์ ๋ถ์ฌํ๊ณ , ๋ณ์๋ ๊ทธ ๊ณต๊ฐ์ ์ฃผ์๋ง ์ฐธ์กฐํ๋๋ก ํ์! ๊ทธ๋์ ์ฐธ์กฐ ์๋ฃํ์ด ํ์ํ๊ฒ ๋ฉ๋๋ค.
๊ฐ์ ํฌ๊ธฐ๊ฐ ์์๋ก ๋ณํ๋ ๊ฒ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด์ ์ฐธ์กฐ์๋ฃํ์ heap์ด๋ผ๋ ๋ณ๋์ ๋ฉ๋ชจ๋ฆฌ ์ ์ฅ๊ณต๊ฐ ์ฌ์ฉ
์ฐธ์กฐ ์๋ฃํ์ ===(strict equality)๋ ์ฃผ์๊ฐ์ด ๊ฐ์์ง๋ฅผ ํ์ธ
console.log([1,2,3] === [1,2,3]);
์ด ์ฝ๋๋ฅผ ์ฝ์ผ๋ฉด์ ์ปดํจํฐ๋ ์ด๋ฏธ ๋๊ฐ์ heap ์ ์ฅ ๊ณต๊ฐ์ ์ฃผ์ ํ๋ณด
๋ฏธ๋ฆฌ ์ฃผ์๊ฐ๊ณผ ๋ฉ๋ชจ๋ฆฌ ๊ฐ์ ์ก์๋๋ค --> [] === [] ๋ true ๊ฐ ๋์ค์ง ์์
4. ๋ค์ ์ฝ๋๊ฐ ์คํ๋ ํ, x.foo์ ๊ฐ์ ๋ฌด์์ธ์ง ๊ณ ๋ฅด์ธ์.
let x = { foo: 3 };
let y = x;
y = 2;
๋ต : 3
let x = { foo: 3 };
let y = x; // ์ฌ๊ธฐ์ y๋ x์ ์ฃผ์๊ฐ ํ ๋น
y = 2; // ํ์ง๋ง ๋ค์ 2๋ฅผ ํ ๋น
์ด ๊ณผ์ ์์ ์ฐธ์กฐ ์๋ฃํ {foo : 3}์๋ ์๋ฌด๋ฐ ์ํฅ์ ๋ฏธ์น์ง ์์๊ธฐ ๋๋ฌธ์
x.foo๋ ๊ทธ๋๋ก 3
5. ๋ค์ ์ฝ๋๊ฐ ์คํ๋ ํ, myArray์ ๊ฐ์ ๋ฌด์์ธ์ง ๊ณ ๋ฅด์ธ์.
let myArray = [2, 3, 4, 5];
let ourArray = myArray;
ourArray[2] = 25;
ourArray = undefined;
๋ต : [2, 3, 25, 5]
- ๋ณ์ ourArray์ myArray์ ์ฃผ์๊ฐ์ด ํ ๋น
- ourArray ์ฃผ์๊ฐ์ ์์นํ ๋ฐฐ์ด์ 2๋ฒ์งธ ์์๋ฅผ 25๋ก ๋ณ๊ฒฝ
(ourArray์ myArray์ ์ฃผ์๊ฐ์ ๊ฐ์์ผ๋ก ํ์ฌ myArray๋ [2, 3, 25, 5]) - ๋ณ์ ourArray์ ์์ ์๋ฃํ undefined๊ฐ ํ ๋น๋์๊ธฐ ๋๋ฌธ์, myArray์ ์ ๊ทผx
(ํ์ง๋ง myArray๋ ๊ทธ๋๋ก [2, 3, 25, 5])
6. ๋ค์ ์ฝ๋๊ฐ ์คํ๋ ํ, player.score์ ๊ฐ์ ๋ฌด์์ธ์ง ๊ณ ๋ฅด์ธ์.
let player = { score: 3 };
function doStuff(obj) {
obj.score = 2;
}
doStuff(player);
๋ต : 2
doStuff(player);
- ํจ์ doStuff์ ๋ณ์ player๋ฅผ ์ธ์๋ก ์ ๋ฌํ์ฌ ํธ์ถ
function doStuff(obj) {
obj.score = 2;
}
- ํจ์๊ฐ ํธ์ถ๋๋ฉด (์คํ๋๋ฉด) ๋งค๊ฐ๋ณ์ obj์ ๋ณ์ player์ ๊ฐ์ด ํ ๋น - { score: 3 }์ ์ฃผ์๊ฐ
- obj.score์ 2๋ฅผ ํ ๋นํ๋ฉด, obj์ player๋ ๊ฐ์ ์ฃผ์๊ฐ์ ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์, player.score๋ 2
7. ๋ค์ ์ฝ๋๊ฐ ์คํ๋ ํ, score ์ ๊ฐ์ ๋ฌด์์ธ์ง ๊ณ ๋ฅด์ธ์.
let score = 80;
function doStuff(value) {
value = 90;
}
doStuff(score)
๋ต : 80
doStuff(score)
ํจ์ doStuff์ score์ ๊ฐ์ ์ธ์๋ก ์ ๋ฌํ์ฌ ์คํ
function doStuff(value) {
value = 90;
}
- ๋งค๊ฐ๋ณ์ value์ score์ ๊ฐ 80์ด ์ ๋ฌ๋๊ณ , value = 90์์ value์ 90์ด ํ ๋น
- ๋ค๋ง ๋ณ์ score์ ๊ฐ 80์, ์ฐธ์กฐ ์๋ฃํ์ด ์๋๊ธฐ ๋๋ฌธ์ ์ฃผ์๊ฐ์ ์ ๋ฌํ์ง ์๊ณ , ๊ฐ ์์ฒด๋ฅผ ๋ณต์ฌํ์ฌ ์ ๋ฌ
- ๋ฐ๋ผ์ ํจ์์์ ์ด๋ค ์ผ์ด ๋ฐ์ํ๋๊ฐ์ ๊ด๋ จ์ด ์์ด score๋ ์ด๊ธฐ์ ํ ๋น๋ ๊ฐ 80์ด ๊ทธ๋๋ก ์ ์ง
'Lecture > ์ฝ๋์คํ ์ด์ธ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SEB FE] section 1 unit 10 (3) ์ค์ฝํ ์ข ํฉ๋ฌธ์ (0) | 2022.09.08 |
---|---|
[SEB FE] section 1 unit 10 (2) ์ค์ฝํ (0) | 2022.09.06 |
[SEB FE] section 1 unit 7 - ๊ณ์ฐ๊ธฐ ๊ธฐ๋ฅ๊ตฌํํ๊ธฐ 1๏ธโฃ ํ๊ธฐ (0) | 2022.09.01 |
[SEB FE] ๊ณ์ฐ๊ธฐ ๋ชฉ์ ๋ง๋ค๊ธฐ (0) | 2022.08.30 |
[SEB FE] Section 1 unit3 (3) - ๋ฐ๋ณต๋ฌธ ๋ฌธ์ ํ๊ธฐ2 [11~16] (0) | 2022.08.28 |