bind
Estimated reading time: 1 minutebind
~ create a function by bind
let user = {
firstName: "John"
};
function func() {
console.log(this.firstName);
}
let funcUser = func.bind(user);
funcUser(); // John
call/apply/bind
call vs apply
let info = 'kamal'
let user = (pro,age) => {
return `${this.info}\'s ${pro} is ${age} `
}
console.log(user.call(info,'age',21))
// kamal's age is 21
apply
- same as call
- but 2nd argument must be an array
let info = 'kamal'
let datas = ['age',21]
let user = (pro,age) => {
return `${this.info}\'s ${pro} is ${age} `
}
console.log(user.apply(info,datas))
// kamal's age is 21