Common methods of a function in JS

In JavaScript, functions themselves are objects, so they have their own built-in methods and properties

1. call(thisArg, ...args)

Calls a function with a specified this context and arguments passed individually.

const person = { name: "Alice" };
function sayHello(age) {
console.log(`Hello, ${this.name}. You are ${age}.`);
}
sayHello.call(person, 30); // "Hello, Alice. You are 30."

2. apply(thisArg, argsArray)

Similar to call, but arguments are passed as an array.

function greet(greeting, name) {
console.log(`${greeting}, ${name}!`);
}

greet.apply(null, ["Hi", "Bob"]); // "Hi, Bob!"

3. bind(thisArg, ...args)

Creates a new function with a specific this context and optionally pre-specified arguments.

function greet(greeting, name) {
console.log(`${greeting}, ${name}!`);
}

const boundGreet = greet.bind(null, "Good morning");
boundGreet("Charlie"); // "Good morning, Charlie!"