Skip to main content
InterviewHubPRO
Section Overview
Q448

Question 448 of 536

Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

84%
JavaScript & V8 Internals (536 Qs)•Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

How do you create your own bind method using either call or apply method?

senior LevelCode ExampleMemory Management

The custom bind function needs to be created on Function prototype inorder to use it as other builtin functions. This custom function should return a function similar to original bind method and the implementation of inner function needs to use apply method call.

The function which is going to bind using custom myOwnBind method act as the attached function(boundTargetFunction) and argument as the object for apply method call.

js
 Function.prototype.myOwnBind = function (whoIsCallingMe) {
   if (typeof this !== "function") {
     throw new Error(this + "cannot be bound as it's not callable");
   }
   const boundTargetFunction = this;
   return function () {
     boundTargetFunction.apply(whoIsCallingMe, arguments);
   };
 };