Coding Challenge Practice - Question 99

Published: (January 10, 2026 at 05:54 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

Problem Description

Implement an extends helper in ES5 that creates a constructor applying both a super type and a sub type, sets up the correct prototype chain for instances, and inherits static properties from the super type.

Solution

Instance prototype inheritance

SubType.prototype = Object.create(SuperType.prototype);
SubType.prototype.constructor = SubType;

Combined constructor

function ExtendedType(...args) {
  SuperType.apply(this, args);
  SubType.apply(this, args);
}

Use SubType.prototype for instances

ExtendedType.prototype = SubType.prototype;

Static inheritance

Object.setPrototypeOf(ExtendedType, SuperType);

Final implementation

const myExtends = (SuperType, SubType) => {
  // Set up prototype inheritance for instances
  SubType.prototype = Object.create(SuperType.prototype);
  SubType.prototype.constructor = SubType;

  // Constructor that applies both SuperType and SubType
  function ExtendedType(...args) {
    SuperType.apply(this, args);
    SubType.apply(this, args);
  }

  // Instances should use SubType.prototype
  ExtendedType.prototype = SubType.prototype;

  // Inherit static properties from SuperType
  Object.setPrototypeOf(ExtendedType, SuperType);

  return ExtendedType;
};
Back to Blog

Related posts

Read more »

React Coding Challenge : TypeHead

Zeeshan Ali !ZeeshanAli-0704https://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws...