Bharat Sahu

Polyfill for bind(), Step-by-step

Polyfill for bind() method
let name = {    firstName: "John",    lastName: "Doe"}
function printFullName () { console.log(this.firstName + ' ' + this.lastName)}
let printName = printFullName.bind(name)
Function.prototype.polyfill_bind = function () {    //...}
    printFullName.polyfill_bind(name)
Function.prototype.polyfill_bind = function () {    return function () {        // ...    }}let showName = printFullName.polyfill_bind(name);showName();
Function.prototype.polyfill_bind = function () {    let context = this;    return function () {        context.call(/* Need to pass arguments */)    }}
Function.prototype.polyfill_bind = function (...args) {    let context = this;    return function () {        context.call(args[0])
}
}
Function.prototype.polyfill_bind = function (...args) {    let context = this;    let params = args.slice(1)    return function (...args2) {        context.apply(args[0], [...params, ...args2] )    }}
    context.call(args[0], ...params, ...args2)
let name = {    firstName: "John",    lastName: "Doe"}
function printFullName (city, country) { console.log(this.firstName + ' ' + this.lastName + ' from ' + city + ', ' + country)}Function.prototype.polyfill_bind = function (...args) { let context = this; let params = args.slice(1) return function (...args2) { context.apply(args[0], [...params, ...args2] ) // Or // context.call(args[0], ...params, ...args2) }}let showName = printFullName.polyfill_bind(name, 'Jabari village')showName('Wakanda');// Output: John Doe from Jabari village, Wakanda

Web App Developer