DEV Community

Dharan Ganesan
Dharan Ganesan

Posted on

5 1 2

Day 12: Function Borrowing

Understanding Function Borrowing:

Function borrowing enables us to use a method from one object and apply it to another object. This can be achieved by invoking a method from one object but setting the this keyword inside the method to point to a different object. It is accomplished through the use of .call(), .apply(), or .bind(), all of which exist to explicitly set this on the method we are borrowing.

  • call method:

The call() method is used to invoke a function with a specified this value and arguments provided individually.

functionName.call(thisArg, arg1, arg2, ...);
Enter fullscreen mode Exit fullscreen mode
const person1 = {
  name: 'Alice',
  greet: function(greeting) {
    console.log(`${greeting}, I'm ${this.name}.`);
  },
};

const person2 = {
  name: 'Bob',
};

person1.greet.call(person2, 'Hello');
// Output: Hello, I'm Bob.
Enter fullscreen mode Exit fullscreen mode
  • apply method:

The apply() method is similar to call(), but it takes an array-like object as its second argument to pass arguments to the function.

functionName.apply(thisArg, [arg1, arg2, ...]);
Enter fullscreen mode Exit fullscreen mode
const person1 = {
  name: 'Alice',
  greet: function(greeting, age) {
    console.log(`${greeting}, I'm ${this.name}, and I'm ${age} years old.`);
  },
};

const person2 = {
  name: 'Bob',
};

person1.greet.apply(person2, ['Hello', 30]);
// Output: Hello, I'm Bob, and I'm 30 years old.
Enter fullscreen mode Exit fullscreen mode
  • bind method:

The bind() method creates a new function with a specified this value and any provided initial arguments. Unlike call() or apply() that executes the function immediately, bind() returns a new function that can be called later.

const newFunction = functionName.bind(thisArg, arg1, arg2, ...);
Enter fullscreen mode Exit fullscreen mode
const person1 = {
  name: 'Alice',
  greet: function(greeting) {
    console.log(`${greeting}, I'm ${this.name}.`);
  },
};

const person2 = {
  name: 'Bob',
};

const greetBob = person1.greet.bind(person2, 'Hi');
greetBob();
// Output: Hi, I'm Bob.
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (3)

Collapse
 
overflow profile image
overFlow • Edited

I don’t know if it’s jus me but this not a beginner lesson. I think it needs extra elaboration and examples.
It’s nice though !

Collapse
 
overflow profile image
overFlow • Edited

What level is this lesson ?! Is it intermediary or advanced ?!

Collapse
 
dhrn profile image
Dharan Ganesan

it is for intermediary level.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more