DEV Community

Discussion on: JS Polyfills asked in Interviews

jakewhelan profile image
Jake Whelan • Edited on

While this sounds like it would be a good answer please do not do this; because that would be a red flag.

The point of this kind of interview exercise is not about whether it’s practical to write a polyfill for a well supported feature. I encourage my teams to use all ES2021 features and we polyfill/transpile support for everything using well known tools and resources (including core-js, as you suggest), but I would ask you this in an interview.

Why? The process of a candidate producing the polyfill gives a lot of clues about their knowledge of the JavaScript language and their problem solving skills.

For example array.prototype.filter in addition to demonstrating knowledge of how that feature works, demonstrates working knowledge of:

  • How polyfills work
  • Feature detection
  • Loops
  • Prototypical inheritance
  • Scope/block scope
  • Function as a first class citizen
  • Array mutation

The algorithmic details of the implementation would have some weight as well, but for me this question is more about probing for language and platform knowledge.

The example for array.prototype.filter in this article (depending on seniority of candidate) wouldn’t be acceptable, because its not a polyfill: it’s a ponyfill.

Without implementing feature detection, removing the first argument (arr), and finally applying this to the Array prototype in the global scope: this is not a 1:1 replacement for array.prototype.filter.

lukeshiru profile image
LUKESHIRU • Edited on

The thing is, you can test all that by asking the candidate to "add a method to all arrays", not a polyfill. The correct answer to a polyfill is to use an off the shelve solution. Also modifying a global prototype is not ideal.

My point mainly is that:

  1. An interview shouldn't be testing something unrelated to the actual position (like those interviews that ask for the definition of "Prototypical inheritance" like we were on highschool). There are folks that are excellent coders, and they don't know the proper name of some things (practical learners), so asking for definitions that you can Google is kinda pointless.
  2. There are better ways for testing this without the need of making a polyfill (which already has a solution in the real world).
Thread Thread
jakewhelan profile image
Jake Whelan • Edited on

Sorry I do get your point and I would never ask for the dictionary definition of prototypical inheritance because as you suggest: it’s fruitless.

However if I asked you to remove the first argument because its redundant and you couldn’t I would have to assume your knowledge of prototypical inheritance is a WIP - and that might be fine, but it depends on seniority.

As for modifying global prototypes: that’s exactly what core-js does. For polyfills it’s standard.

The polyfill: (proto: true)

The documentation for proto arg:

prototype as the target for the “export”:

Thread Thread
marzelin profile image
Marc Ziel

If you don't know the proper names for programming concepts how will you be able to communicate efficiently with the rest of the team? And what are those better ways for testing? I hope it's not a random coding challenge because it isn't any better imho.

Thread Thread
lukeshiru profile image
LUKESHIRU • Edited on

Edit: This answer was supposed to go to this comment but for some reason it went to another one (I answered from the notifications on DEV).

Yup, maybe I wasn't clear and what I said got mixed up, but those are two different things:

  • You shouldn't ask for pollyfills in interviews, because in the real world we already have solutions for those (as core-js).
  • You can test for something like "adding something new to an existing prototype, but that's not something you should do in general, unless that prototype is completely under your control.

So to test all the things you mentioned in your previous comment (even if we nowadays we have classes in JS), you could simple give the candidate a piece of code like this:

function Example (initialCount = 0) {
  this.count = initialCount;

// ???

const example = new Example();

example.increment().count; // should be 1
example.increment(2).count; // should be 3
example.decrement(3).count; // should be 0
Enter fullscreen mode Exit fullscreen mode

And ask them to implement those increment and decrement methods. Why would you ask them to modify the prototype of a global like Array or Object when is a bad practice in the real world, or to implement a polyfill when that's done by packages like core-js? We should be interviewing for real world scenarios.

About this comment I was talking about terms like "hoisting", "asi" and stuff like that, which many folks are aware of but don't know the "actual name" for it, and it doesn't actually matter in the day-to-day work.

abhishekraj272 profile image
Abhishek Raj Author

In this article, I was trying to show how those method works and I have also written at the end to use the Prototypal Inheritance way.

I have written it as partial polyfill.