Understand how the apply method works in JavaScript and cover step by step-by-step explanation of the apply method polyfill in JavaScript to understand its internal implementation.
Anuj Sharma
Last Updated Nov 29, 2024
apply method works similarly to the call method and is used to pass the dynamic context while function invocation. apply method takes the function parameters as an Array rather than comma-separate values in call method. Here is an example
Passing context to the function with parameters
// function
function getStudentInfo(stream, college) {
console.log(
`${this.firstName} ${this.lastName} has ${stream} stream in ${college}.`
);
}
const student1 = {
firstName: 'Anuj',
lastName: 'Sharma',
passingYear: 2012,
getPassingYear: function () {
console.log(`Passing year is ${this.passingYear}`);
},
};
const student2 = {
firstName: 'Ravi',
lastName: 'Kumar',
passingYear: 2018,
};
// Pass student1 and student2 as context
getStudentInfo.apply(student1, ['IT', 'Poornima']);
// Output: Anuj Sharma has IT stream in Poornima.
getStudentInfo.apply(student2, ['CS', 'JEC']);
// Output: Ravi Kumar has CS stream in JEC.
If nothing is passed as a context then, globalThis is used by default as context. In browser env globalThis points to "window" and in node globalThis points to "global" object.
const firstName= 'Anuj';
const lastName= 'Sharma';
// function
function getStudentInfo(stream, college) {
console.log(
`${this.firstName} ${this.lastName} has ${stream} stream in ${college}.`
);
}
// By default window context got passed
getStudentInfo.apply();
// Output: Anuj Sharma has undefined stream in undefined.
Here is the step-by-step explanation of the apply polyfill, this polyfill is similar to the call polyfill with the only difference with parameters passed as an Array,
Step 1: Edge case handling, to check if the second parameter is array. In the case when the second parameter is not array it throws an Error.
Step 2: Capture the currentContext, this will be either the context passed as the first parameter of the apply function. In case nothing is passed currentContext gets the globalThis as context value, which is "window" in the browser & "global" in Nodejs env. globalThis will provide appropriate value as per the execution environment.
Step 3: Create a temporary new Symbol key to avoid overriding any existing key in the currentContext (Nothing but an object). Assign the function's context ("this") to the newly added temporary key, on which the call function is invoked.
Step 4: Now, since the invoker function is part of the currentContext, It can access values from the current context. Now call the function and pass the arguments (Array of parameters got as part of the apply).
Step 5: Delete the symbol key, to avoid any pollution in the currentContext.
Step 6: Return the result which got as part of function execution as the final result.
Function.prototype.customApply = function (context, args = []) {
// Step 1: Edge case handling
if (!Array.isArray(args)) {
throw new Error("Second parameter should be array.");
}
// Step 2: Assign context
let currentContext = context || globalThis;
// Step 3: Create a temporary new symbol key & assign the function
let newProp = Symbol();
currentContext[newProp] = this;
// Step 4: function invocation
let result = currentContext[newProp](...args);
// Step 5: Delete temporary key
delete currentContext[newProp];
return result;
};
Advertisement
Advertisement
Alok Kumar Giri
Last Updated Jun 2, 2025
Code snippet examples which will help to grasp the concept of Hoisting in JavaScript, with solutions to understand how it works behind the scene.
Anuj Sharma
Last Updated Oct 28, 2025
Find the step-by-step explanation of the useFetch custom hook in React that helps in fetching the data from an API and handling loading, error states.
Anuj Sharma
Last Updated Oct 2, 2025
Explore Polyfill for map, filter and reduce array methods in JavaScript. A detailed explanation of Map, filter and reduce polyfills in JS helps you to know the internal working of these array methods.
Anuj Sharma
Last Updated Aug 3, 2025
Explore the implementation of setTimeout in JavaScript with a detailed explanation for every step. Understand all scenarios expected to implement the setTimeout polyfill.
Anuj Sharma
Last Updated Oct 26, 2025
In this post, we will going to cover the step-by-step implementation of Infinite Currying Sum with a code example. This is one of the most common JavaScript Interview questions.
Anuj Sharma
Last Updated Oct 26, 2025
Understand the step-by-step implementation of Infinite Currying Multiplication in JavaScript with a code example.
Subscribe to FrontendGeek Hub for the frontend interview preparation, interview experiences, curated resources and roadmaps.
© 2024 FrontendGeek. All rights reserved