DEV Community

Cover image for Function Call and Apply Methods
Bello Osagie
Bello Osagie

Posted on β€’ Edited on

2 1

Function Call and Apply Methods


call() Method

The call() method accepts an object and optionally argument list.

See the syntax below:

object.method.call(obj1[, arg1, ...argN])
Enter fullscreen mode Exit fullscreen mode

call() method with a parameter object

The call() method is used on an object. The parameter passed to the function is also an object.

See the examples below:

Without call() method.

const person = {
  firstName: "Osagie",
  lastName: "Bello",
  fullName() {
    return `Hello ${this.lastName} ${this.firstName}!`;
  }
};

console.log(person.fullName()); // Hello Bello Osagie!
Enter fullscreen mode Exit fullscreen mode


With call() method.

const person = {
  fullName() {
    return `Hello ${this.lastName} ${this.firstName}!`;
};

const person1 = {
  firstName: "Osagie",
  lastName: "Bello"
};

const person2 = {
  firstName: "Richard",
  lastName: "Jury"
};

console.log(person.fullName.call(person1)); Hello Bello Osagie!
console.log(person.fullName.call(person2));  // Hello Richard Jury!
Enter fullscreen mode Exit fullscreen mode

call() method with arguments

The call() method is used on an object with arguments also passed to the function.

const person = {
  fullName(country, city) {
    return `${this.firstName} ${this.lastName}, ${country} ${city}`;
  }
};

const person1 = {
  firstName: "Osagie",
  lastName: "Bello"
};

const person2 = {
  firstName: "Richard",
  lastName: "Jury"
};

console.log(person.fullName.call(person1, "Nigeria", "Lagos"));  
// Osagie Bello, Nigeria Lagos
console.log(person.fullName.call(person2, "UK", "Oxford"));  
// Richard Jury, UK Oxford
Enter fullscreen mode Exit fullscreen mode

apply() Method

The apply() method accepts an object and optionally array instead of an argument list.

See the syntax below:

object.method.apply(obj1[, array])
Enter fullscreen mode Exit fullscreen mode

apply() method with arguments array

Not only an object be passed to the apply method, but it also accepts an array.

const person = {
  fullName(country, city) {
    return `${this.firstName} ${this.lastName}, ${country} ${city}`;
  }
};

const person1 = {
  firstName: "Osagie",
  lastName: "Bello"
};

const person2 = {
  firstName: "Richard",
  lastName: "Jury"
};

console.log(person.fullName.call(person1, "Nigeria", "Lagos"));  
// Osagie Bello, Nigeria Lagos

console.log(person.fullName.apply(person2, ["UK", "Oxford"]));  
// Richard Jury, UK Oxford
Enter fullscreen mode Exit fullscreen mode

Happy coding!!!


image.png


image.png

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay