const say = function () {
console.log(this);
console.log("Hello, my name is" + this.name);
};
say();
이 코드를 실행하게 되면 this 는 window 객체로 나타나게 된다.
하지만 this 에 window 객체 대신 다른 값들을 넣어주고 싶을 때 쓰는 함수가 바로 call, apply, bind 이다.
call() 메소드는 주어진 this 값 및 각각 전달된 인수와 함께 함수를 호출, apply() 와 거의 동일하지만, call() 은 인수 목록을, apply() 는 인수 배열 하나를 받는다는 점이 가장 큰 차이점 입니다.
func.call(thisArg[, arg1[, arg[, ...]]])
thisArg: func 호출에 제공되는 this의 값
arg1, arg2: func이 호출되어야 하는 인수
const obj = { name: 'chaelin' }
const say = function (city) {
console.log(`Hello, my name is ${this.name}, I live in ${city}`);
};
say("seoul") // Hello, my name is , I live in Seoul
say.call(obj, "seoul") // Hello, my name is chaelin, I live in Seoul
const obj = { name: 'chaelin' }
const say = function (city, age) {
console.log(`Hello, my name is ${this.name}, I live in ${city}. And I'm ${age} years old`);
};
say("seoul", 26) // Hello, my name is , I live in seoul. And I'm 20 years old
say.call(obj, "seoul", 26) // Hello, my name is chaelin, I live in seoul. And I'm 20 years old
func.apply(thisArg, [argsArray])
앞서 말했듯, call() 과의 차이점은 단일 배열을 받는다는 것입니다.
thisArg: func 호출에 제공되는 this의 값
argsArray: func이 호출되어야 하는 인수를 지정하는 유사 배열 객체
const obj = { name: 'chaelin' }
const say = function (city) {
console.log(`Hello, my name is ${this.name}, I live in ${city}`);
};
say.apply(obj, ["seoul"]) // Hello, my name is chaelin, I live in Seoul
const obj = { name: 'chaelin' }
const say = function (city, age) {
console.log(`Hello, my name is ${this.name}, I live in ${city}. And I'm ${age} years old`);
};
say.apply(obj, ["seoul", 20]) // Hello, my name is chaelin, I live in seoul. And I'm 20 years old
bind() 함수가 call(), apply() 와 다른 점은 함수를 실행하지 않는다는 점이다. 그 대신 bound 함수를 리턴한다. call(), apply()의 경우 한 번 사용하면 끝이지만 bind를 사용하면 bound 함수는 특정한 this를 가지고 있기 때문에 나중에 사용해도 된다는 장점이 있다.
func.bind(thisArg[, arg1[,arg2[, ...]]])
const obj = { name: 'chaelin' }
const say = function (city) {
console.log(`Hello, my name is ${this.name}, I live in ${city}`);
};
const boundSay = say.bind(obj)
boundSay("seoul") // Hello, my name is chaelin, I live in Seoul
즉, call() 은 custom this 를 지정할 수 있었고, apply() 는 call() 과 유사하지만 인자를 배열을 받는다는 점에서 차이가 있다. 마지막으로 bind()는 custom this를 영구히 가지는 함수를 만들 수 있다.
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Function/call
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
https://wooooooak.github.io/javascript/2018/12/08/call,apply,bind/
Image by Bianca Van Dijk from Pixabay