apply, bind, call

HG·2022년 10월 28일
0

js개념

목록 보기
2/2

const obj = {name : 'HG'};

const say = function (city) {
console.log(my city is ${city} , my name is ${this.name} )
}
say("seoul") // this = window
// 첫번째 인자 = this로 세팅하고 싶은 객체를 넘겨준다.
say.call(obj, "seoul") //this = obj
say.apply(obj, ["seoul"]) // this = obj
//call apply 차이 parameter를 입결하는 방식 apply는 배열에 넣어야한다.

const sayMySelf = function (city, old) {
console.log(my name ${this.name}, my city is ${city} my old is ${old})
}

sayMySelf.apply(obj, ["seoul",25 ])
// apply가 더 유리한 경우 배열을 이용중인 경우에 하나씩 나누어서 spread를 이용할 필요가 없어야한다.

const sayHG = say.bind(obj);

sayHG("seoul")

profile
Making Body, Making Food, Making Co?de

0개의 댓글