const mike = {
name: "Mike";
}
function showThisName() {
console.log(this.name);
}
function update(birthYear, occupation){
this.birthYear = birthYear;
this.occupation = occupation;
}
call ๋ฉ์๋์ ์ฒซ ๋ฒ์งธ ๋งค๊ฐ๋ณ์ == this ๋ก ์ฌ์ฉํ ๊ฐ
showThisName.call(mike); // Mike
update.call(mike, 1996, "singer");
console.log(mike); //{name: "Mike", birthYear: 1996, occupation: "singer"}
call๊ณผ ์ฌ์ฉ๋ฒ์ด ๋น์ทํ์ง๋ง ํจ์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐฐ์ด๋ก ๋ฐ์.
๋์ ๋ฐฉ์์ ๊ฐ์. ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ๋ ๋ฐฉ๋ฒ๋ง ๋ค๋ฆ.
๋ค์๊ณผ ๊ฐ์ด ๋ฐฐ์ด ํํ๋ ์คํ๋ ๋ ์ฐ์ฐ์๋ฅผ ์ด์ฉํด ๋งค๊ฐ๋ณ์๋ก ๋ฃ์ด์ค ์ ์์๋ค.
const nums = [1,2,4,5,6,10]; const minNum = Math.min(...nums) // 1 const maxNum = Math.max(...nums) // 10
๋ฐฐ์ด ๋งค๊ฐ๋ณ์๋ ์ฐจ๋ก๋๋ก ์ธ์๋ก ์ฌ์ฉ๋จ
apply๋ฅผ ์ฌ์ฉํด๋ณด์.
const nums = [1,2,4,5,6,10]; const minNum = Math.min.apply(null,nums) // 1 const maxNum = Math.max.apply(null,nums) // 10
Math.min
,Math.max
๋ this๊ฐ ํ์ํ์ง ์์ ์๋ฌด๊ฐ์ด๋ ๋ฃ์ด์ฃผ์ด๋ ๋๋ค.
this ๊ฐ์ ์๊ตฌ์ ์ผ๋ก ๋ฐ๊ฟ์ค ์ ์๋ค.
const mike = {
name: "Mike";
}
function update(birthYear, occupation){
this.birthYear = birthYear;
this.occupation = occupation;
}
const updateMike = update.bind(mike);
updateMike(1980, "police");
console.log(mike); // {name: "Mike", birthYear: 1980, occupation: "police"}
const User = {
name: "Mike",
showName: function () {
console.log(`hello, ${this.name}`);
},
};
user.showName(); // hello, Mike
let fn = user.showName();
fn(); // hello,
method - .์์ด this!
this๋ฅผ ์์ด๋ฒ๋ ธ๊ธฐ ๋๋ฌธ์ name์ด ์ถ๋ ฅ๋์ง ์์.
let boundFn = fn.bind(user);
boundFn(); // hello, Mike