CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

πŸ“ž Function.prototype.apply() Polyfill

The apply() method invokes a function with a specified this context and arguments as an array. Similar to call but with array arguments.

The method in JavaScript is similar to , except that it takes arguments as an array. -- 1️⃣ Understanding Native behavior: πŸ“Œ Difference from : β†’ Arguments are passed individually. β†’ Arguments are passed as an array. -- 2️⃣ Polyfill for We need to: Attach the function to the (context). Pass the arguments as an array. Remove the temporary function reference. -- 3️⃣ Testing the Polyfill -- 4️⃣ Dry Run Step-by-Step Let's break it down for: -- πŸ“Œ Step 1: Call inside refers to . (). . -- πŸ“Œ Step 2: Function Definition Breakdown Inside : βœ… is (a function), so no error. -- πŸ“Œ Step 3: Set . -- πŸ“Œ Step 4: Attach Function Temporarily A unique property is added to : -- πŸ“Œ Step 5: Invoke Function Equivalent to: Since is now , it prints: -- πŸ“Œ Step 6: Cleanup Removes the temporary function from , restoring it to: -- πŸ“Œ Final Execution Summary Action is called 2️⃣ is created and assigned to 4️⃣ Temporary function property is deleted | βœ… Works just like the native method! -- <!-quiz-start --Q1: What is the main difference between and ? [ ] call() is faster [ ] apply() can change , call() cannot [x] call() takes arguments individually, apply() takes them as an array [ ] apply() returns a new function, call() invokes immediately Q2: Why is a used as the function key in the polyfill? [ ] For better performance [x] To avoid property name collisions on the context object [ ] To make the function enumerable [ ] It's required by the JavaScript spec Q3: What does return? [ ] The context object [ ] A new function [x] The return value of the invoked function [ ] undefined always <!-quiz-end --
JavaScriptPolyfills
🎯 Array.prototype.at() Polyfill
medium
βœ… Array.prototype.every() Polyfill
medium
πŸ”² Array.prototype.fill() Polyfill
medium
πŸ” Array.prototype.filter() Polyfill
medium
πŸ”Ž Array.prototype.find() Polyfill
medium
πŸ”’ Array.prototype.findIndex() Polyfill
medium
πŸ”™ Array.prototype.findLast() Polyfill
medium
πŸ”™ Array.prototype.findLastIndex() Polyfill
medium
πŸ“‹ Array.prototype.flat() Polyfill
medium
πŸ” Array.prototype.includes() Polyfill
medium
πŸ”’ Array.prototype.indexOf() Polyfill
medium
βœ… Array.isArray() Polyfill
medium
πŸ—ΊοΈ Array.prototype.map() Polyfill
hard
βž– Array.prototype.pop() Polyfill
medium
βž• Array.prototype.push() Polyfill
easy
πŸ”„ Array.prototype.reduce() Polyfill
medium
πŸ”„ Array.prototype.reverse() Polyfill
hard
⬅️ Array.prototype.shift() Polyfill
hard
πŸ”˜ Array.prototype.some() Polyfill
medium
πŸ”€ Array.prototype.sort() Polyfill
hard
➑️ Array.prototype.unshift() Polyfill
hard
πŸ“ž Function.prototype.apply() Polyfill
medium
πŸ”— Function.prototype.bind() Polyfill
hard
πŸ“ž Function.prototype.call() Polyfill
medium
22 of 24
LibraryJavaScriptPolyfills37 of 61

πŸ“ž Function.prototype.apply() Polyfill

jspolyfillsmedium

The apply method in JavaScript is similar to call, except that it takes arguments as an array.


1️⃣ Understanding Function.prototype.apply

Native behavior:

function greet(age, city) {
  console.log(`Hello, my name is ${this.name}, I am ${age} years old, and I live in ${city}.`);
}

const person = { name: "Alice" };
greet.apply(person, [25, "New York"]);

πŸ“Œ Difference from call :

  • call(context, arg1, arg2, arg3, ...) β†’ Arguments are passed individually.
  • apply(context, [arg1, arg2, arg3, ...]) β†’ Arguments are passed as an array.

2️⃣ Polyfill for apply

We need to:

  • Attach the function to the thisArg (context).
  • Pass the arguments as an array.
  • Remove the temporary function reference.
Function.prototype.myApply = function (context, args) {
  if (typeof this !== "function") {
    throw new TypeError("myApply can only be used on functions");
  }

  context = context || globalThis; // Default to global (window in browser, global in Node.js)

  const fnKey = Symbol(); // Unique key to avoid property collisions
  context[fnKey] = this; // Assign function to context
  
  const result = context[fnKey](...(args || [])); // Invoke function with spread operator
  
  delete context[fnKey]; // Cleanup

  return result;
};

3️⃣ Testing the Polyfill

function greet(age, city) {
  console.log(`Hello, my name is ${this.name}, I am ${age} years old, and I live in ${city}.`);
}

const person = { name: "Bob" };

greet.myApply(person, [30, "Los Angeles"]);
// Output: Hello, my name is Bob, I am 30 years old, and I live in Los Angeles.

4️⃣ Dry Run Step-by-Step

Let's break it down for:

greet.myApply(person, [30, "Los Angeles"]);

πŸ“Œ Step 1: Call myApply

greet.myApply(person, [30, "Los Angeles"]);
  • this inside myApply refers to greet.
  • context = person ({ name: "Bob" }).
  • args = [30, "Los Angeles"].

πŸ“Œ Step 2: Function Definition Breakdown

Inside myApply:

Function.prototype.myApply = function (context, args) {
  if (typeof this !== "function") {
    throw new TypeError("myApply can only be used on functions");
  }

βœ… this is greet (a function), so no error.


πŸ“Œ Step 3: Set context

context = context || globalThis;
  • context = person.

πŸ“Œ Step 4: Attach Function Temporarily

const fnKey = Symbol();
context[fnKey] = this;
  • A unique property is added to person:
    {
      name: "Bob",
      [Symbol(fnKey)]: function greet(age, city) { ... }
    }

πŸ“Œ Step 5: Invoke Function

const result = context[fnKey](...(args || []));
  • Equivalent to:
    person ;
  • Since this is now person, it prints:
    Hello, my name is Bob, I am 30 years old, and I live in Los Angeles.
    

πŸ“Œ Step 6: Cleanup

delete context[fnKey];
  • Removes the temporary function from person, restoring it to:
    { name: "Bob" }

πŸ“Œ Final Execution Summary

StepAction
1️⃣greet.myApply(person, [30, "Los Angeles"])is called
2️⃣context = person
3️⃣Symbol(fnKey)is created and assigned to person[Symbol(fnKey)] = greet
4️⃣person is invoked, printing "Hello, my name is Bob, I am 30 years old, and I live in Los Angeles."
5️⃣Temporary function property is deleted

βœ… Works just like the native apply method!


Quick Quiz

Test your understanding with 3 quick questions

Q1What is the main difference between `call()` and `apply()`?
Q2Why is a `Symbol` used as the function key in the polyfill?
Q3What does `apply()` return?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna