*call, apply, bind: ์๋ฐ์คํฌ๋ฆฝํธ์์๋ ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ ์ธ์๋ ํจ์๋ฅผ ์ด๋์ ์ด๋ป๊ฒ ํธ์ถํ๋์ง์ ๊ด๊ณ์์ด this๊ฐ ๋ฌด์์ธ์ง ์ง์ ํ ์ ์์
*call: ๋ชจ๋ ํจ์์์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ฉฐ this๋ฅผ ํน์ ๊ฐ์ผ๋ก ์ง์ ํ ์ ์์
ํจ์๋ฅผ ํธ์ถํ๋ฉด์ call์ ์ฌ์ฉํ๊ณ , this๋ก ์ฌ์ฉํ ๊ฐ์ฒด๋ฅผ ๋๊ธฐ๋ฉด ํด๋นํจ์๊ฐ ์ฃผ์ด์ง ๊ฐ์ฒด์ ๋ฉ์๋์ธ ๊ฒ์ฒ๋ผ ์ฌ์ฉํ ์ ์์
call์ ์ฒซ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ this๋ก ์ฌ์ฉํ ๊ฐ์ด๊ณ ๋งค๊ฐ๋ณ์๊ฐ ์์ผ๋ฉด ๊ทธ ๋งค๊ฐ๋ณ์๋ฅผ ํธ์ถํ๋ ํจ์๋ก ์ ๋ฌ๋จ
const mike = {
name: "Mike",
};
const tom = {
name: "Tom",
};
function showThisName(){
console.log(this.name);
} //์ฌ๊ธฐ์ this๋ window๋ฅผ ๊ฐ๋ฆฌํด
showThisName();
showThisName.call(mike);//Mike
์์) ์ ๋ฐ์ดํธ ํจ์
- ๊ฐ์ฒด์ ์ ๋ณด๊ฐ ์ ๋ฐ์ดํธ ๋จ. ์ฒซ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ this ๊ฐ์ด๊ณ , ๋๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ถํฐ ํจ์๊ฐ ์ฌ์ฉํ ๋งค๊ฐ๋ณ์๋ค์ ์์๋๋ก ์ ์ ๊ฒ.
const mike = {
name: "Mike",
};
const tom = {
name: "Tom",
};
function showThisName(){
console.log(this.name);
}
function update(birthYear, occupation){
this.birthYear = birthYear;
this.occupation = occupation;
};
update.call(mike, 1999, 'singer')
console.log(mike); //{name: 'Mike', birthYear: 1999, occupation: 'singer'}
*apply: ํจ์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ์ ์ธํ๋ฉด call๊ณผ ์์ ํ ๊ฐ์. call์ ์ผ๋ฐ์ ์ธ ํจ์์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋งค๊ฐ๋ณ์๋ฅผ ์ง์ ๋ฐ์ง๋ง, apply๋ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐฐ์ด๋ก ๋ฐ์.
- []๋ก ๋ฌถ์ด์ฃผ์ง ์์ผ๋ฉด ์๋ฌ๋ธ
const mike = {
name: "Mike",
};
const tom = {
name: "Tom",
};
function showThisName(){
console.log(this.name);
}
function update(birthYear, occupation){
this.birthYear = birthYear;
this.occupation = occupation;
};
update.apply(mike, [1999, 'singer'])
console.log(mike); //{name: 'Mike', birthYear: 1999, occupation: 'singer'}
update.apply(tom, [2000, 'teacher'])
console.log(tom); //{name: 'Tom', birthYear: 2000, occupation: 'teacher'}
- ๋ฐฐ์ด ์์๋ฅผ ํจ์ ๋งค๊ฐ๋ณ์๋ก ์ฌ์ฉํ ๋ ์ ์ฉ
-> ... ์ฌ์ฉํ ๊ฒฝ์ฐ
const nums = [3,10,1,6,4];
const minNum = Math.min(...nums);
const maxNum = Math.max(...nums);
console.log(minNum); //1
console.log(maxNum); //10
-> apply ์ฌ์ฉํ ๊ฒฝ์ฐ: ๋๋ฒ์งธ ๋งค๊ฐ๋ณ์ ๋ฐฐ์ด๋ก ์ ๋ฌํ๋ฉด ๊ทธ ์์๋ค์ ์ฐจ๋ก๋๋ก ์ธ์๋ก ์ฌ์ฉํจ
null์ this๋ก ์ฌ์ฉํ ๊ฐ์ธ๋ฐ ์ฌ๊ธฐ์๋ Math.min์ด๋ Math.max๋ ๋ฑํ this๊ฐ์ด ํ์ํ์ง ์์ ์๋ฌด๊ฐ ๋ฃ์ ๊ฒ
const nums = [3,10,1,6,4];
const minNum = Math.min.apply(null,nums);
// = Math.min.apply(null, [3,10,1,6,4])
const maxNum = Math.max.apply(null,nums);
console.log(minNum); //1
console.log(maxNum); //10
โ call๋ ์ฌ์ฉ ๊ฐ๋ฅ( ...nums๋ก ์)
const nums = [3,10,1,6,4];
const maxNum = Math.max.call(null,...nums);
// = Math.min.call(null, [3,10,1,6,4])
console.log(minNum); //1
console.log(maxNum); //10
*bind: ํจ์์ this๊ฐ์ ์๊ตฌํ ๋ฐ๊ฟ ์ ์์
const mike = {
name: "Mike",
};
function update(birthYear, occupation){
this.birthYear = birthYear;
this.occupation = occupation;
};
const upadteMike = update.bind(mike); //์ด ํจ์๋ ํญ์ mike๋ฅผ this๋ก ๋ฐ์
upadteMike(1999, "singer");
console.log(mike); //{name: 'Mike', birthYear: 1999, occupation: 'singer'}
์์)
- fn์ ํ ๋นํ ๋ this๋ฅผ ์์ด๋ฒ๋ฆฐ ๊ฒฝ์ฐ ๊ฐ์ด ๋จ์ง ์์. (๋ฉ์๋๋ .showName ์์ ์๋๊ฒ this(user)์) ํธ์ถํ ๋ fn๋ง ํธ์ถํ๊ณ this๊ฐ ์๋ ์ํ
const user = {
name: "Mike",
showName: function(){
console.log(`hello, ${this.name}`);
},
};
user.showName(); //hello, Mike
let fn = user.showName;
fn(); // hello,
- call์ ์ฌ์ฉํด this๋ก ์ฌ์ฉํ ๊ฐ user๋ฅผ ๋ฃ์
const user = {
name: "Mike",
showName: function(){
console.log(`hello, ${this.name}`);
},
};
user.showName(); //hello, Mike
let fn = user.showName;
fn.call(user); // hello, Mike
-apply๋ฅผ ๋ฃ์ด๋ ๋์ผ
const user = {
name: "Mike",
showName: function(){
console.log(`hello, ${this.name}`);
},
};
user.showName(); //hello, Mike
let fn = user.showName;
fn.apply(user); // hello, Mike
- bind๋ฅผ ์ด์ฉํด ์ ํจ์ ๋ง๋ค๊ธฐ
const user = {
name: "Mike",
showName: function(){
console.log(`hello, ${this.name}`);
},
};
user.showName(); //hello, Mike
let fn = user.showName;
fn.apply(user); // hello, Mike
let boundFn = fn.bind(user);
boundFn();
'js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
javascript ํด๋์ค(class) (0) | 2022.11.01 |
---|---|
javascript ์์, ํ๋กํ ํ์ (Prototype) (0) | 2022.10.31 |
javascript setTimeout / setInterval (0) | 2022.10.31 |
javascript ํด๋ก์ (Closure) (0) | 2022.10.28 |
javascript ๋๋จธ์ง ๋งค๊ฐ๋ณ์, ์ ๊ฐ ๊ตฌ๋ฌธ(Rest parameters, Spread syntax) (0) | 2022.10.28 |