// 1. ํจ์๋ ๋ฌด๋ช
์ ๋ฆฌํฐ๋ด๋ก ์์ฑํ ์ ์๋ค.
// 2. ํจ์๋ ๋ณ์์ ์ ์ฅํ ์ ์๋ค.
// ๋ฐํ์(ํ ๋น ๋จ๊ณ)์ ํจ์ ๋ฆฌํฐ๋ด์ด ํ๊ฐ๋์ด ํจ์ ๊ฐ์ฒด๊ฐ ์์ฑ๋๊ณ ๋ณ์์ ํ ๋น๋๋ค.
const increase = function (num) {
return ++num;
}
const decrease = function (num) {
return --num;
}
// 2. ํจ์๋ ๊ฐ์ฒด์ ์ ์ฅํ ์ ์๋ค.
const auxs = { increase, decrease };
// 3. ํจ์์ ๋งค๊ฐ๋ณ์์ ์ ๋ฌํ ์ ์๋ค.
// 4. ํจ์์ ๋ฐํ๊ฐ์ผ๋ก ์ฌ์ฉํ ์ ์๋ค.
function makeCounter(aux) {
let num = 0;
return function() {
num = aux(num);
return num;
};
}
// 3. ํจ์๋ ๋งค๊ฐ๋ณ์์๊ฒ ํจ์๋ฅผ ์ ๋ฌํ ์ ์๋ค.
const increaser = makeCounter(auxs.increase);
console.log(increaser()); // 1
console.log(increaser()); // 2
//3. ํจ์๋ ๋งค๊ฐ๋ณ์์๊ฒ ํจ์๋ฅผ ์ ๋ฌํ ์ ์๋ค.
const increaser = makeCounter(auxs.decrease);
console.log(decrease()); // -1
console.log(decrease()); // -2
ํจ์๋ ๊ฐ์ ์ฌ์ฉํ ์ ์๋ ๊ณณ์ด๋ผ๋ฉด ์ด๋์๋ ์ง ๋ฆฌํฐ๋ด๋ก ์ ์ํ ์ ์์ผ๋ฉฐ ๋ฐํ์์ ํจ์ ๊ฐ์ฒด๋ก ํ๊ฐ๋๋ค.
์ผ๊ธ ๊ฐ์ฒด๋ก์ ํจ์๊ฐ ๊ฐ์ง๋ ๊ฐ์ฅ ํฐ ํน์ง์ ์ผ๋ฐ ๊ฐ์ฒด์ ๊ฐ์ด ํจ์์ ๋งค๊ฐ๋ณ์์ ์ ๋ฌํ ์ ์์ผ๋ฉฐ, ํจ์์ ๋ฐํ๊ฐ์ผ๋ก ์ฌ์ฉํ ์๋ ์๋ค๋ ๊ฒ์ด๋ค.
ํจ์๋ ๊ฐ์ฒด์ด์ง๋ง ์ผ๋ฐ ๊ฐ์ฒด์๋ ์ฐจ์ด๊ฐ ์๋ค. ์ผ๋ฐ ๊ฐ์ฒด๋ ํธ์ถํ ์ ์์ง๋ง ํจ์ ๊ฐ์ฒด๋ ํธ์ถํ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ ํจ์ ๊ฐ์ฒด๋ ์ผ๋ฐ ๊ฐ์ฒด์๋ ์๋ ๊ณ ์ ์ ํ๋กํผํฐ๋ฅผ ์์ ํ๋ค.
ํจ์ ๊ฐ์ฒด์ ํ๋กํผํฐ arguments, caller, length, name, prototype ํ๋กํผํฐ๋
๋ชจ๋ ์ผ๋ฐ ๊ฐ์ฒด์๋ ์๋ ํจ์ ๊ฐ์ฒด ๊ณ ์ ์ ๋ฐ์ดํฐ ํ๋กํผํฐ์ด๋ค.
ํจ์ ๊ฐ์ฒด์ arguments ํ๋กํผํฐ ๊ฐ์ arguments ๊ฐ์ฒด๋ค.
arguments ๊ฐ์ฒด๋ ํจ์ ํธ์ถ ์ ์ ๋ฌ๋ ์ธ์๋ค์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ์ํ ๊ฐ๋ฅํ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ด๋ฉฐ, ํจ์ ๋ด๋ถ์์ ์ง์ญ ๋ณ์์ฒ๋ผ ์ฌ์ฉ๋๋ค. ํจ์ ์ธ๋ถ์์๋ ์ฐธ์กฐX
function multiply(x, y) {
console.log(arguments);
return x * y;
}
// ์๋ฐ์คํฌ๋ฆฝํธ๋ ํจ์์ ๋งค๊ฐ๋ณ์์ ์ธ์์ ๊ฐ์๊ฐ ์ผ์นํ๋์ง ํ์ธํ์ง ์๋๋ค.
// ํจ์ ํธ์ถ ์ ๋งค๊ฐ๋ณ์ ๊ฐ์๋งํผ ์ธ์๋ฅผ ์ ๋ฌํ์ง ์์๋ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์๋๋ค..
console.log(multiply()); // NaN
console.log(multiply(1)); // NaN
console.log(multiply(1, 2)); // 2
console.log(multiply(1, 2, 3)); // 2
arguments ๊ฐ์ฒด๋ ๋งค๊ฐ๋ณ์ ๊ฐ์๋ฅผ ํ์ ํ ์ ์๋ ๊ฐ๋ณ ์ธ์ ํจ์ ๊ตฌํ์ ์ ์ฉํ๋ค.
function sum() {
let res = 0;
// arguments ๊ฐ์ฒด๋ length ํ๋กํผํฐ๊ฐ ์๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ด๋ฏ๋ก for ๋ฌธ์ผ๋ก ์ํํ ์ ์๋ค.
for (let i = 0; i < arguments.length; i++) {
res += arguments[i];
}
return res;
}
console.log(sum()); // 0
console.log(sum(1, 2)); // 3
console.log(sum(1, 2, 3)); // 6
ES6์์๋ Rest ํผ๋ผ๋ฏธํฐ๋ฅผ ๋์ ํ์๋ค.
// ES6 Rest parameter
function sum(...args){
return args.reduce((pre,cur) => pre + cur, 0);
}
console.log(sum(1,2)); // 3
console.log(sum(1,2,3,4,5)); // 15
ES6 Rest ํผ๋ผ๋ฏธํฐ์ ๋์
์ผ๋ก ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ์์๋ agrguments ๊ฐ์ฒด์ ์ค์์ฑ์ด ์ด์ ๊ฐ์ง๋ ์๋ค.
caller ํ๋กํผํฐ๋ ECMAScript ์ฌ์์ ํฌํจ๋์ง ์์ ๋นํ์ค ํ๋กํผํฐ๋ค.
์ดํ ํ์คํ๋ ์์ ๋ ์๋ ํ๋กํผํฐ์ด๋ฏ๋ก ์ฌ์ฉํ์ง ๋ง๊ณ ์ฐธ๊ณ ๋ก๋ง ์์๋์.
ํจ์ ๊ฐ์ฒด์ caller ํ๋กํผํฐ๋ ํจ์ ์์ ์ ํธ์ถํ ํจ์๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
function foo(func) {
return func();
}
function bar() {
return 'caller : ' + bar.caller;
}
// ๋ธ๋ผ์ฐ์ ์์์ ์คํํ ๊ฒฐ๊ณผ
console.log(foo(bar)); // caller : function foo(func) {...}
console.log(bar()); // caller : null
ํจ์ ๊ฐ์ฒด์ length ํ๋กํผํฐ๋ ํจ์๋ฅผ ์ ์ํ ๋ ์ ์ธํ ๋งค๊ฐ๋ณ์์ ๊ฐ์๋ฅผ ๊ฐ๋ฆฌํจ๋ค
arguments ๊ฐ์ฒด์ length ํ๋กํผํฐ์ ํจ์ ๊ฐ์ฒด์ length ํ๋กํผํฐ์ ๊ฐ์ ๋ค๋ฅผ ์ ์์ผ๋ฏ๋ก ์ฃผ์ํด์ผ ํ๋ค. arguments ๊ฐ์ฒด์ length ํ๋กํผํฐ๋ ์ธ์์ ๊ฐ์๋ฅผ ๊ฐ๋ฆฌํค๊ณ , ํจ์ ๊ฐ์ฒด์ length ํ๋กํผํฐ๋ ๋งค๊ฐ๋ณ์์ ๊ฐ์๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
function foo() {}
console.log(foo.length); // 0
function bar(x) {
return x;
}
console.log(bar.length); // 1
function baz(x, y) {
return x * y;
}
console.log(baz.length); // 2
ํจ์ ๊ฐ์ฒด์ name ํ๋กํผํฐ๋ ํจ์ ์ด๋ฆ์ ๋ํ๋ธ๋ค. ES6์์ ์ ์ ํ์ค์ด ๋์๋ค.
// ๊ธฐ๋ช
ํจ์ ํํ์
var namedFunc = function foo() {};
console.log(namedFunc.name); // foo
// ์ต๋ช
ํจ์ ํํ์
var anonymousFunc = function() {};
// ES5: name ํ๋กํผํฐ๋ ๋น ๋ฌธ์์ด์ ๊ฐ์ผ๋ก ๊ฐ๋๋ค.
// ES6: name ํ๋กํผํฐ๋ ํจ์ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ๋ณ์ ์ด๋ฆ์ ๊ฐ์ผ๋ก ๊ฐ๋๋ค.
console.log(anonymousFunc.name); // anonymousFunc
// ํจ์ ์ ์ธ๋ฌธ(Function declaration)
function bar() {}
console.log(bar.name); // bar
๋ชจ๋ ๊ฐ์ฒด๋ [[Prototype]]์ด๋ผ๋ ๋ด๋ถ ์ฌ๋กฏ์ ๊ฐ๋๋ค.
__proto__ ํ๋กํผํฐ๋ [[Prototype]] ๋ด๋ถ ์ฌ๋กฏ์ด ๊ฐ๋ฆฌํค๋ ํ๋กํ ํ์ ๊ฐ์ฒด์ ์ ๊ทผํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ์ ๊ทผ์ ํ๋กํผํฐ๋ค. ๋ด๋ถ ์ฌ๋กฏ์๋ ์ ๊ทผํ ์ ์๊ณ ๊ฐ์ ์ ์ธ ์ ๊ทผ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ ๊ฒฝ์ฐ์ ํํ์ฌ ์ ๊ทผํ ์ ์๋ค.
prototype ํ๋กํผํฐ๋ ์์ฑ์ ํจ์๋ก ํธ์ถํ ์ ์๋ ํจ์ ๊ฐ์ฒด, ์ฆ constructor๋ง์ด ์์ ํ๋ ํ๋กํผํฐ๋ค.
์ผ๋ฐ ๊ฐ์ฒด์ ์์ฑ์ ํจ์๋ก ํธ์ถํ ์ ์๋ non-constructor์๋ prototype ํ๋กํผํฐ๊ฐ ์๋ค.
prototype ํ๋กํผํฐ๋ ํจ์๊ฐ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์์ฑ์ ํจ์๋ก ํธ์ถ๋ ๋ ์์ฑ์ ํจ์๊ฐ ์์ฑํ ์ธ์คํด์ค์ ํ๋กํ ํ์ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.