์ค๋ ๊ณต๋ถํ๋ฉด์ ์ธ์ฌ์ดํธ, ๊ฐ๋ ์์ฝ ๋ฑ
๊ธฐ๋ณธ ๋ฐ์ดํฐ ํ์ ์ ์ค์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ๋ค๋ฃธ
์ฐธ์กฐํ ๋ฐ์ดํฐ ํ์ ์ ์ค์ ๋ฐ์ดํฐ๊ฐ ์๋ ์์น์ ์ฃผ์๋ฅผ ์ ์ฅํ๊ณ ๋ค๋ฃธ
ํจ์ ํธ์ถ ๋ฐฉ์: ์ฐธ์กฐ์ ์ํ ํธ์ถ (Call by Reference) โ ์ฐธ์กฐํ์
์์ ์ฝ๋
js ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ
function add10(data1, data2) {
data1 += 10;
data2[0] += 10;
console.log("ํจ์ ๋ด๋ถ์์ ํธ์ถ", data1, data2[0]);
}
let d1 = 80; // => ์์ํ์
, Number
let d2 = [80]; // => ์ฐธ์กฐํ์
, array(object)
console.log("ํจ์ ํธ์ถ ์ด์ ", d1, d2[0]);
add10(d1, d2);
console.log("ํจ์ ํธ์ถ ์ดํ", d1, d2[0]);
call stack
๋ชจ๋ ๋ณ์๋ ์ฌ๊ธฐ์ ์ ์ฉ๋จ
๋ฐ์์ ๋ถํฐ ๋ฐ์ดํฐ๊ฐ ํ๋์ฉ ์์ด๋ ๊ณณ
ํจ์๊ฐ ๋๋๋ฉด stack frame์ด ์ญ์์ผ๋ก ์ฌ๋ผ์ง (์ ์ผ ๋ง์ง๋ง์ ์์๋ ๊ฒ๋ถํฐ ์ฌ๋ผ์ง)
ex01-37-02 - call stack
console.log("1. ํ๋ก๊ทธ๋จ ์์.");
function a(n1) {
console.log("2. a ์์.", n1);
const n2 = b(n1);
console.log("7. a ์ข
๋ฃ.", n2);
}
function b(n3) {
console.log("3. b ์์.", n3);
const n4 = c(n3); // 3.5๋ฒ
console.log("6. b ์ข
๋ฃ.", n4);
return n4;
}
function c(n5) {
console.log("4. c ์์.", n5);
const n6 = n5 + 10;
console.log("5. c ์ข
๋ฃ.", n6);
return n6;
}
a(10);
console.log("8. ํ๋ก๊ทธ๋จ ์ข
๋ฃ.");
๐ง Call Stack ํ๋ฆ ์์ฝ (์๊น ๋ฐ๋ผ๊ฐ๊ธฐ)
ํ๋ก๊ทธ๋จ ์์ (console.log 1)a(10) ํธ์ถ โ a ์์ (console.log 2)b(n1) ํธ์ถ โ b ์์ (console.log 3)c(n3) ํธ์ถ โ c ์์ (console.log 4)c() ๋ด๋ถ์์ n6 = n5 + 10console.log("5. c ์ข
๋ฃ.") โ ๊ฐ ๋ฐํ (n6)b()๋ก ๋์์์ console.log("6. b ์ข
๋ฃ.") โ ๊ฐ ๋ฐํ (n4)a()๋ก ๋์์์ console.log("7. a ์ข
๋ฃ.") โ ์ข
๋ฃconsole.log("8. ํ๋ก๊ทธ๋จ ์ข
๋ฃ.")๐ฏ ์์ฝ ๋น์
heap
๐ง ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ ์ดํด
data1์ ๋ณต์ฌ(pass-by-value)๋ผ.data1 = 80์ด ๋๊ณ , data1 += 10์ ํด๋ d1์๋ ์ํฅ์ด ์์ด.data1์ ํจ์ ์์์๋ง 90์ด ๋๊ณ , ๋ฐ๊นฅ์ d1์ ์ฌ์ ํ 80์ด์ผ.d2๋ ์ฐธ์กฐํ์
(Array/Object)d2์ ์ฐธ์กฐ๊ฐ(์ฃผ์)์ด data2๋ก ๊ณต์ (pass-by-reference)๋ผ.
์ฆ, data2[0] += 10์ ์ค์ ๋ฐฐ์ด์ ๊ฐ์ธ d2[0]์ ๋ฐ๊ฟ.
๊ทธ๋์ ๋ฐ๊นฅ์ d2[0]๋ ๊ฐ์ด 90์ผ๋ก ๋ฐ๋.
๋น์
๐ฑ ๋์๋ฝ ๋น์
๐งโโ๏ธ d1์ "์ง์ ๊ฐ์ ธ๊ฐ ๋์๋ฝ"
์์ํ์
, ์ฆ pass-by-value (๊ฐ ๋ณต์ฌ)!๐งโโ๏ธ d2๋ "๋์๋ฝ ํต์งธ๋ก ๋น๋ ค์ค ๊ฑฐ"
์ฐธ์กฐํ์
, ์ฆ pass-by-reference (์ฐธ์กฐ ์ ๋ฌ)var age = 20; // ์ ์ญ ๋ณ์
(window.)year = 10; // ์ ์ญ ๋ณ์
function getAge(year){ // ์ง์ญ ๋ณ์
var age = 30; // ์ง์ญ ๋ณ์
(window.)myAge = age + year; // ์ ์ญ ๋ณ์
(window.)alert(myAge);
(window.)console.log(myAge);
}(window.)myAge = age + year;window.์ ๋ถ์ฌ์ ์๋์ฐ ๊ฐ์ฒด์ ์๋์ ์๋ค๊ณ ๋ช
์ํด์ฃผ์fill()
๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ์ง์ ํ ๊ฐ์ผ๋ก ์ฑ์ด๋ค.
// ๋ฌธ๋ฒ: array.fill(value, start, end)
// value: ๋ฐฐ์ด์ ์ฑ์ธ ๊ฐ
// start(์ ํ): ์์ ์ธ๋ฑ์ค (๊ธฐ๋ณธ๊ฐ 0)
// end(์ ํ): ๋ ์ธ๋ฑ์ค (๊ธฐ๋ณธ๊ฐ array.length)
const arr = new Array(5);
arr.fill(7);
console.log(arr); // [7, 7, 7, 7, 7]
// ํน์ ๊ตฌ๊ฐ๋ง ์ฑ์ฐ๊ธฐ
const nums = [1, 2, 3, 4, 5];
nums.fill(0, 2, 4);
console.log(nums); // [1, 2, 0, 0, 5]
join()
๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ์ฐ๊ฒฐํด ํ๋์ ๋ฌธ์์ด๋ก ๋ง๋ ๋ค.
// ๋ฌธ๋ฒ: array.join(separator)
// separator(์ ํ): ์์ ์ฌ์ด์ ๋ค์ด๊ฐ ๊ตฌ๋ถ์ (๊ธฐ๋ณธ๊ฐ์ ์ผํ)
const fruits = ['์ฌ๊ณผ', '๋ฐ๋๋', '๋ธ๊ธฐ'];
console.log(fruits.join()); // '์ฌ๊ณผ,๋ฐ๋๋,๋ธ๊ธฐ'
console.log(fruits.join(' ')); // '์ฌ๊ณผ ๋ฐ๋๋ ๋ธ๊ธฐ'
console.log(fruits.join(' - ')); // '์ฌ๊ณผ - ๋ฐ๋๋ - ๋ธ๊ธฐ'
split()
๋ฌธ์์ด์ ์ง์ ๋ ๊ตฌ๋ถ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋๋์ด ๋ฐฐ์ด๋ก ๋ณํํ๋ค.
// ๋ฌธ๋ฒ: string.split(separator, limit)
// separator: ๋ถํ ๊ธฐ์ค (๋ฌธ์์ด ๋๋ ์ ๊ท์)
// limit(์ ํ): ๋ฐํํ ๋ถํ ํญ๋ชฉ ์์ ์ ํ
const sentence = '์๋
ํ์ธ์, ์๋ฐ์คํฌ๋ฆฝํธ ๊ณต๋ถ ์ค์
๋๋ค';
const words = sentence.split(' ');
console.log(words); // ['์๋
ํ์ธ์,', '์๋ฐ์คํฌ๋ฆฝํธ', '๊ณต๋ถ', '์ค์
๋๋ค']
const csv = '๊น์ฒ ์,30,๊ฐ๋ฐ์';
const person = csv.split(',');
console.log(person); // ['๊น์ฒ ์', '30', '๊ฐ๋ฐ์']
// ๋ชจ๋ ๋ฌธ์๋ฅผ ๊ฐ๋ณ ์์๋ก
const chars = '์๋
ํ์ธ์'.split('');
console.log(chars); // ['์', '๋
', 'ํ', '์ธ', '์']
includes()
๋ฐฐ์ด์ ํน์ ์์๊ฐ ํฌํจ๋์ด ์๋์ง ํ์ธํ๊ณ true๋๋ false๋ฅผ ๋ฐํํ๋ค.
// ๋ฌธ๋ฒ: array.includes(searchElement, fromIndex)
// searchElement: ์ฐพ์ ์์
// fromIndex(์ ํ): ๊ฒ์์ ์์ํ ์์น (๊ธฐ๋ณธ๊ฐ 0)
const numbers = [1, 2, 3, 4, 5];
console.log(numbers.includes(3)); // true
console.log(numbers.includes(6)); // false
// ํน์ ์์น๋ถํฐ ๊ฒ์
console.log(numbers.includes(1, 2)); // false (์ธ๋ฑ์ค 2๋ถํฐ ๊ฒ์)
// ๋ฌธ์์ด ๋ฐฐ์ด์์๋ ์ฌ์ฉ ๊ฐ๋ฅ
const fruits = ['์ฌ๊ณผ', '๋ฐ๋๋', '๋ธ๊ธฐ'];
console.log(fruits.includes('๋ฐ๋๋')); // true
console.log(fruits.includes('์๋ฐ')); // false