-
Notifications
You must be signed in to change notification settings - Fork 2
[JS] ํจ์
- ํจ์๋
this, ๋ฉ์๋, ํ๋กํ ํ์ , ๋ชจ๋ํ, ์ค์ฝํ, ์คํ ์ปจํ์คํธ, ์ฝ๋ก์
๋ฑ์ ์๋ฐ์คํฌ๋ฆฝํธ ํต์ฌ๊ฐ๋ ๊ณผ ๊น์ ๊ด๋ จ์ด ์์ - ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ดํดํ๋ ค๋ฉด ํจ์์ ๋ํ ์ดํด๊ฐ ํ์์ ์ผ๋ก ํ์ํจ
- ํจ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ, ์ ์ง ๋ณด์์ ํธ์์ฑ, ์ฝ๋์ ๊ฐ๋ ์ฑ์ ํฅ์์ํด
- ํจ์๋ ๊ฐ์ฒด์(์ผ๊ธ ๊ฐ์ฒด)
- ๊ฐ์ฒด์ ๋ค๋ฅธ ์ ์
callable
ํ๋ค๋ ๊ฒ => ๋ด๋ถ ๋ฉ์๋[[ Call ]]
๊ณผ ๊ด๋ จ - ์์ฑ์ ํจ์๋
callable
ํ๋ฉด์ ๋์์constructor
์ => ๋ด๋ถ ๋ฉ์๋[[ Construct ]]
๊ณผ ๊ด๋ จ
-> ํ๋กํผํฐ ์ดํธ๋ฆฌ๋ทฐํธ ํ์ธํ๊ธฐ1:console.dir(ํจ์์ด๋ฆ)
-> ํ๋กํผํฐ ์ดํธ๋ฆฌ๋ทฐํธ ํ์ธํ๊ธฐ2: ๊ฐ์ฒด์ ๋ชจ๋ ์์ฑ๋ค์ ์ค๋ช ์๋ฅผ ๋ฐํํ๋ ํจ์ ์ฌ์ฉํด์ ์ฐ์ด๋ณด๊ธฐ - ES6์ด์ ์ ํจ์๋ค์ ๋ชจ๋
callable
ํ๋ฉด์ ๋์์constructor
์ด์์ผ๋ ES6๋ถํฐ ๊ตฌ๋ถํ๊ธฐ ์์ํจ
- ํจ์๋ฅผ ์ ์ํ๋ค๊ณ ํํํ๋ ์ด์
- ์ ์ธ๊ณผ ์ ์์ ๊ฐ์ฅ ํฐ ์ฐจ์ด: ๋ฉ๋ชจ๋ฆฌ์ ํ ๋น ์ฌ๋ถ
- ํจ์ ์ ์ ๊ณผ์
- ํ๊ฐ๊ณผ์ ์์ ํจ์ ์ ์ธ๋ฌธ์ ์๋ณ์๊ฐ ์๋ฌต์ ์ผ๋ก ์์ฑ๋จ
- ํจ์ ๊ฐ์ฒด๊ฐ ํ ๋น๋จ(๋ฉ๋ชจ๋ฆฌ์ ๋ก๋)
- ๋ฐ๋ผ์ ํจ์๋ ์ ์ธํ๋ค๊ณ ํํํ์ง ์๊ณ ์ ์ํ๋ค๊ณ ํํํจ
- ECMAScript์์๋ ๋ณ์์๋
์ ์ธํ๋ค
, ํจ์์๋์ ์ํ๋ค
๋ ํํ์ ์
-
ํจ์๋ฆฌํฐ๋ด๋ก ์ ์ํ ๊ฒฝ์ฐ, ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ด ์ด๋ฅผ ๊ฒฝ์ฐ์ ๋ฐ๋ผ ํจ์ ์ ์ธ๋ฌธ/ ํจ์ ํํ์์ผ๋ก ํด์ํ๋ค.
-
๊ฐ์ผ๋ก ํ๊ฐ๋๋๋ ์๋๋์ ๋ฐ๋ผ ๋ฌ๋ผ์ง
-
ํจ์ ์ ์ธ๋ฌธ: ํจ์ ํธ์ด์คํ
์:function b(){};
- ํ๊ฐ๊ณผ์ ์์ b๋ผ๋ ํจ์๊ฐ์ฒด๊ฐ ์์ฑ๋จ(์์ง์ด b๋ฅผ ์๋ณ์๋ก ์ฌ์ฉํจ, b๋ ์๋ ํจ์ ๋ด๋ถ์์ ์ฌ์ฉํ๋ ์๋ณ์์)
- ์ ์ธ๋ฌธ ์ด์ ์ ํธ์ถ์ ํด๋ ๋ฌธ์ ๊ฐ ์์ => ๋ก์ง์ ๋ฌธ์ ๊ฐ ์์ผ๋ฏ๋ก ๊ถ์ฅx
-
ํจ์ ํํ์: ๋ณ์ ํธ์ด์คํ
์:var c = function d(){};
- ํ๊ฐ๊ณผ์ ์์ c๋ผ๋ ๋ณ์๊ฐ ์์ฑ๋จ(var์ด๊ธฐ ๋๋ฌธ์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น ํ undefined๋ก ์ด๊ธฐํ)
- ์ ์ธ๋ฌธ ์ด์ ์ ํธ์ถํ๋ฉด TypeError ๋ฐ์(undefined ์ํ)
- ํ ๋น๋ฌธ์ด ์คํ๋๋ ์์ ์ (๋ฐํ์) function d๊ฐ c์ ํ ๋น ๋จ
์: let func = (arg1, arg2, ...argN) => expression
=> ํญ์ ์ต๋ช
ํจ์
- ๊ธฐ์กด ํจ์์ ๋ค๋ฅธ ์
constructor
๊ฐ ๋ ์ ์์ => ๋ด๋ถ ๋ฉ์๋์[[ Construct ]]
๋ฅผ ๊ฐ์ง๊ณ ์์ง ์์
- ๊ฐ๋ตํ๋ ํํ(
=>
fat arrow) - this ๋ฐ์ธ๋ฉ
-
์๋ ํจ์๋ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ this ๋ฐ์ธ๋ฉ์ด ๊ฒฐ์ ๋จ(๋์ )
- ์ผ๋ฐํจ์๋ก ํธ์ถํ๋ ๊ฒฝ์ฐ: ์ ์ญ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ(strict mode์์๋ undefined๊ฐ ๋ฐ์ธ๋ฉ ๋จ)
- ๋ฉ์๋๋ก ํธ์ถํ๋ ๊ฒฝ์ฐ: ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด(
abc.print()
์ ๊ฒฝ์ฐprint
๋ฉ์๋์ this๋abc
๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ) - ์์ฑ์ ํจ์๋ก์ ํธ์ถ: ์์ฑ์ ํจ์๊ฐ ๋ฏธ๋์ ์์ฑํ ์ธ์คํด์ค(
const add = new Func()
์ ๊ฒฝ์ฐadd
)
-
์ฝ๋ฐฑํจ์์์์ this ๋ฐ์ธ๋ฉ ๋ฌธ์
class Person { // ํด๋์ค ๋ด๋ถ๋ strict mode๋ก ์คํ๋จ
constructor(name) {
this.name = name;
}
announce(arr) {
console.log(this); // ์ฌ๊ธฐ์ this๋ jueun ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํด
const that = this; // this๋ฅผ ํํผ์ํด
// map์ ์ฝ๋ฐฑํจ์๋ก ์ผ๋ฐ ํจ์๋ฅผ ์ ๋ฌํ ๊ฒฝ์ฐ
return arr.map(function (item) {
// return `Hi ${item}, my name is ${that.name}.`; // ํํผ์ํค๋ ๋ฐฉ๋ฒ
// return `Hi ${item}, my name is ${this.name}.`; // Q: ๊ฒฐ๊ณผ๋?
});
}
}
this.name = "error"; // ์ ์ญ์ ๋ฑ๋ก
const jueun = new Person("jueun");
const greeting = jueun.announce(["a", "b", "c", "d"]);
console.log(greeting);
- ์ด๊ฒ์ ํ์ดํ ํจ์๋ก ํด๊ฒฐํ ์ ์์(์๋์ ์ผ๋ก ์ค๊ณ๋ ๊ฒ)
// map์ ํ์ดํ ํจ์๋ฅผ ์ ๋ฌํ ๊ฒฝ์ฐ
// return arr.map((item) => {
// return `Hi ${item}, my name is ${this.name}.`;
// });
-
prototype ํ๋กํผํฐ๊ฐ ์์. ์ฆ, non-constructor์
func : ํ์ดํํจ์
add : ํจ์์ ์ธ๋ฌธ
- ์ด prototype ํ๋กํผํฐ๋ ์์ฑ์ ํจ์๋ก ํธ์ถ ํ ์ ์๋ ํจ์๋ง ๊ฐ์ง
- ์ด ํ๋กํผํฐ๋ ํจ์๊ฐ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์์ฑ์ ํจ์์ ๋์๊ณผ ๊ด๋ จ์๋ ๊ฐ์ฒด์(์์์ ์ฌ์ฉ๋จ)
-
arguments ๊ฐ์ฒด ์์ฑ x
arguments ๊ฐ์ฒด mdn
function func1(a, a, a) {
console.log(arguments[0]);
// expected output: 1
console.log(arguments[1]);
// expected output: 2
console.log(arguments[2]);
// expected output: 3
}
const arrow1 = (a,a,a) => {}; // Error: Duplicate parameter name not allowed in this context
const arrow2 = (a) => {
console.log(arguments[0]);
};
func1(1, 2, 3);
arrow2(1);
โ ์ข ๋ ๊ฐ๋ตํ ํจ์๋ฅผ ์ ์ธํ ์ ์์ผ๋ ๊ธฐ์กด์ ํจ์ ํํ์์ ๋์ฒดํ๊ธฐ ์ํ ๋ฌธ๋ฒ์ ์๋(๋ค๋ฅธ ๋ฐฉ๋ฒ)
[์ฐธ๊ณ ]
- ๋ค๋ฅธ ๊ฐ์ฒด๋ค์๊ฒ ์ ์ฉ ๊ฐ๋ฅํ ์ฐ์ฐ์ ๋ชจ๋ ์ง์ํ๋ ๊ฐ์ฒด๋ฅผ ๋งํจ
- ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์์
type
์์ ๋ฌ/๋ฐํ/ํ ๋น
ํ ์ ์๋ ๊ฒฝ์ฐ => ํด๋นtype
์ ์ผ๊ธ๊ฐ์ฒด๋ก ๊ฐ์ฃผ
- ํ ๋น ๊ฐ๋ฅ(๋ณ์๋ ์๋ฃ๊ตฌ์กฐ์ ์ ์ฅํ ์ ์์)
- ์ ๋ฌ ๊ฐ๋ฅ(ํจ์์ ๋งค๊ฐ๋ณ์์ ์ ๋ฌํ ์ ์์)
- ๋ฐํ ๊ฐ๋ฅ(ํจ์์ ๋ฐํ๊ฐ์ผ๋ก ์ฌ์ฉํ ์ ์์)
"ํจ์๊ฐ ์ผ๊ธ๊ฐ์ฒด๋ผ์ ์ข์ ์ ์?" => flexibility! ๋ค์ํ ํ๋ก๊ทธ๋๋ฐ ํจ๋ฌ๋ค์์ ์ ์ฉํ ์ ์์
- ํจ์๋ฅผ ๋ณ์์ ํ ๋นํ์ฌ ํธ์ถํ์ง ์๊ณ ์ ๋ฌํ๊ฑฐ๋ ๊ฐ์ฒด์ ๋ฉ์๋๋ก ํ ๋นํ๋ ๋ฑ
flexible
ํ๊ฒ ์ฌ์ฉํ ์ ์์ - ํจ์๋ฅผ ๋งค๊ฐ๋ณ์๋ก ์ ๋ฌํ ์ ์์ผ๋ฏ๋ก ๋ฐ๋ณต๋๋ ๋ถ๋ถ์ ์ค์ผ ์ ์์(์ด๋ฒคํธ์ ์ฝ๋ฐฑํจ์ ์ ๋ฌ ๋ฑ)
function showText(e) {
//show specific text
}
$('form').on('submit',showText);
$('a').on('click',showText);
- ํจ์๋ฅผ ๋ฐํํ ์ ์๊ธฐ ๋๋ฌธ์ ๊ณ ์ฐจ ํจ์์ ์ฌ์ฉ์ด ๊ฐ๋ฅํด์ง
- Higher-order function: ๋ค๋ฅธ ํจ์๋ฅผ ์ ๋ฌ์ธ์๋ก ๋ฐ๊ฑฐ๋ ํจ์์คํ์ ๊ฒฐ๊ณผ๋ฅผ ํจ์๋ก ๋ฐํํ๋ ํจ์
- ์ผ๊ธ๊ฐ์ฒด ์์ฑ๊ณผ ํด๋ก์ ๋๋ฌธ์ ์ปค๋ง๊ณผ ๋ฉ๋ชจ์ด์ ์ด์ ์ด ๊ฐ๋ฅํด์ง
- ์ด์ด์ ๊ณต๋ถํ๋ฉด ์ข์ ์ฃผ์ :
currying
,๋ฉ๋ชจ์ด์ ์ด์
,ํ๋กํ ํ์
- ๊ฐ์ธ์ ์ผ๋ก ๊ถ๊ธํ ์ฃผ์ :
์๋ฐ์คํฌ๋ฆฝํธ์ Loose Typing
- ๋ฉ์ปค๋์ฆ? how it works? ๊ฐ๋ฐ์์์ด