Function ๊ฐ์ฒด
ํจ์๋ฅผ ๋ํ๋ด๋ ๊ฐ์ฒด.
Function() ์์ฑ์๋ ์๋ก์ด
Function ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฉฐ, ์์ฑ์๋ฅผ ํธ์ถํ์ฌ ๋์ ์ผ๋ก ํจ์๋ฅผ ์์ฑํ ์ ์๋ค.
Function() ์์ฑ์๋ก ํจ์๋ฅผ ๋ง๋ค ๊ฒฝ์ฐ ๋ณด์ ๋ฌธ์ ์ eval๊ณผ ๋น์ทํ ์ ์ฌ(๋ ์ฌ๊ฐํ) ์ฑ๋ฅ ๋ฌธ์ ๊ฐ
๋ฐ์ํ ์ ์๋ค. ํ์ง๋ง, ์ ์ญ ๋ฒ์์์ ์ฝ๋๋ฅผ ์คํํ ์ ์๊ณ , ์ฝ๋๋ฅผ ์ต์ํ ํ ์ ์๋ค.
Function ๊ฐ์ฒด
- apply() ์ฃผ์ด์ง this ๊ฐ๊ณผ ๋ฐฐ์ด(๋๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด)๋ก ์ ๊ณต๋๋ arguments๋ก ํจ์๋ฅผ ํธ์ถํ๋ค. ๋งค๊ฐ๋ณ์ : thisArg : fun๋ฅผ ํธ์ถํ๋๋ฐ ์ ๊ณต๋ this์ ๊ฐ์ด๋ฉฐ, ๋ฉ์๋๊ฐ ๋น ์๊ฒฉ ๋ชจ๋ ์ฝ๋๋ด์ ํจ์์ผ ๊ฒฝ์ฐ, null๊ณผ undefined๋ ์ ์ญ ๊ฐ์ฒด๋ก ๋์ฒด๋๋ค.
- call() : ์ฃผ์ด์ง this ๊ฐ ๋ฐ ๊ฐ๊ฐ ์ ๋ฌ๋ ์ธ์์ ํจ๊ป ํจ์๋ฅผ ํธ์ถํ๋ค. apply()๊ตฌ๋ฌธ๊ณผ ์ ์ฌํ๋ฉฐ, ์ฐจ์ด์ ์ call์ ํจ์์ ์ ๋ฌ๋ ์ธ์ ๋ฆฌ์คํธ๋ฅผ ๋ฐ๋๋ฐ ๋นํด, apply()๋ ์ธ์ ๋ฐฐ์ด ํ๋๋ฅผ ๋ฐ๋๋ค๋ ์ ์ด๋ค.
</head>
<body>
<h3>apply(),call()</h3>
<script type="text/javascript">
var sum = function (a,b,c) {
return a+b+c;
};
var s;
// ํจ์ ํธ์ถ ๋ฐฉ๋ฒ -------------
// 1) ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ์ผ๋ก ํจ์ ํธ์ถ
s=sum(1,2,3);
console.log(s);
// 2) aplly() ํจ์๋ฅผ ์ด์ฉํ ํจ์ ํธ์ถ
// apply(thisArg[,argArray]) : ์ฃผ์ด์ง this ๊ฐ๊ณผ ๋ฐฐ์ด๋ก ์ ๊ณตํ๋ ์ธ์๋ก ํจ์ ํธ์ถ
s = sum.apply(null,[1,2,3]);
console.log(s);
// 3) call() ํจ์๋ฅผ ์ด์ฉํ ํจ์ ํธ์ถ
// call(thisArg[,agr1,arg2,...]): ์ฃผ์ด์ง this ๊ฐ๊ณผ ๊ฐ๊ฐ ์ ๋ฌ๋ ์ธ์๋ก ํจ์ ํธ์ถ
s = sum.call(null,1,2,3);
console.log(s);
console.log('-----------------------')
var m = Math.max(5,6,3,7,8,3);
console.log(m);
var nums=[5,6,3,7,8,3];
m = Math.max(nums);
console.log(m); // NaN
m = Math.max.apply(null,nums);
console.log(m);
</script>
</body>
</html>
</head>
<body>
<h3>apply(),call()</h3>
<script type="text/javascript">
var obj = {
name : '์ด์๋ฐ',
sayName : function() {
console.log(this.name); // this ํ์
}
};
var obj2 = {
name:'์คํ๋ง'
};
obj.sayName();
//call(thisArg[,agr1,arg2,...])
// thisArg : ํจ์๋ฅผ ํธ์ถํ ๋ ์ ๊ณต๋ this
obj.sayName.call(null); // ์๋ฌด๊ฒ๋ ์ถ๋ ฅ๋์ง ์๋๋ค. this๊ฐ null ์ด๋ฏ๋ก ์๋ฌด๊ฒ๋ ์ถ๋ ฅ ์๋จ
obj.sayName.call(obj);
obj.sayName.call(obj2); // ์คํ๋ง
</script>
</body>
</html>
</head>
<body>
<h3>apply(),call()</h3>
<script type="text/javascript">
function sub() {
for (let i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
sub(1, '์๋ฐ', 2);
console.log('------------------------');
function sub2() {
console.dir(arguments); // Arguments(3), ์ ์ฌ๋ฐฐ์ด
console.dir([]); // Array(0)
//cnosole.log(arguments.join);
// join()์ Array์ ํจ์
}
sub2(1, '์๋ฐ', 2);
//Array ๊ฐ์ฒด์ Join
function sub3(){
console.log(Array.prototype.join.call(arguments));
}
sub3(1,'์๋ฐ',2);
</script>
</body>
</html>
</head>
<body>
<h3>bind</h3>
<script type="text/javascript">
var obj = {
name : '์ด์๋ฐ',
sayName : function () {
console.log(this.name);
}
};
var obj2={
name : '์คํ๋ง'
};
//bind(thisArg[,arg1,arg2,arg3,...])
// : ํธ์ถํ ๋ this๋ฅผ ์ ๊ณต๋ ๊ฐ์ผ๋ก ์ค์ ํ๋ ์๋ก์ด ํจ์๋ฅผ ๋ง๋ฆ
var fn = obj.sayName.bind(obj2);
fn();
</script>
</body>
</html>
'๐ป > Javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
(50) [JavaScript] 6์ผ JavaScript ๊ฐ์ฒด - ํด๋์ค (1) | 2024.04.28 |
---|---|
(49) [JavaScript] 5์ผ JavaScript ๊ฐ์ฒด - ์์ฑ, ๊ตฌ์กฐ๋ถํด (1) | 2024.04.28 |
(48) [JavaScript] 4์ผ JavaScript Array (0) | 2024.04.24 |
(47) [JavaScript] 3์ผ JavaScript ๋ด์ฅ๊ฐ์ฒด (0) | 2024.04.23 |
(47) [JavaScript] 3์ผ JavaScript ํจ์&์์ธ์ฒ๋ฆฌ (0) | 2024.04.22 |