JS BIND-APPLY-CALL
const person = {
name: 'Владилен'
}
function info(phone, email) {
console.log(`Имя: ${this.name}, Тел.:${phone}, Email: ${email}`)
}
// ES6
function bind(fn, context, ...rest) {
return function(...args) {
// return fn.apply(context, rest.concat(args))
return fn.call(context, ...rest.concat(args))
}
}
bind(info, person)('12345', 'v@mail.ru')
bind(info, person, '12345')('v@mail.ru')
bind(info, person, '12345', 'v@mail.ru')()
// Call
function call(fn, context, ...args) {
const uniqId = Date.now().toString()
context[uniqId] = fn
const result = context[uniqId](...args)
delete context[uniqId]
return result
}
// call(info, person, '1234', 'c@mail.ru')
// Apply
function apply(fn, context, args) {
const uniqId = Date.now().toString()
context[uniqId] = fn
const result = context[uniqId](...args)
delete context[uniqId]
return result
}
apply(info, person, ['1234', 'c@mail.ru'])