A more detailed explanation is given beneath the example code.
const alex = {
firstname: "Alex",
surname: "Surname",
}
function nameYourself(...args) {
console.log(`${this.firstname} ${this.surname}`);
console.log('args : ', args);
}
Function.prototype.binding = function(target, ...boundArgs) {
// the function to be bound.
const boundFunction = this;
// the binding wrapper-function.
return function (...args) {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
boundFunction.apply(target, [...boundArgs, ...args])
}
}
nameYourself.binding(alex, 'Hello')(alex.firstname, alex.surname);
.as-console-wrapper { max-height: 100%!important; top: 0; }
Since the custom bind functionality binding should be implemented via Function.prototype it will be called itself as a method of every function object. Thus, a binding's this context will be a function.
The latter then shall be executed within a custom this context. For the OP's example this context is intended to be the target object alex.
In order to achieve the OP's task the binding implementation needs to return a wrapper function that inside its function body does delegate the bound function to the target object via a functions apply method.
bindingsupposed to do?(nameyourself)(), which resetsthistoundefinedandalexisn't being passed to anything.smth.__proto__.bindedfunct = this;smthisalex. You assignnameyouyselfto a property onalex.__proto__. You then return that which you assigned. For all intents and purposes,bindingjust doesreturn thiswith the side effect of adding a property toalex'__proto__. That doesn't "bind" anything.