Emmanuel Akinfulubi

JavaScriptFundamental (ES6), part 2

How super is resolved in JavaScript

79 super is resolved from the method, not from this

hard

Build a three-level chain where each describe calls super.describe(). Then copy the middle one onto an unrelated object and call it there: work out which parent it reaches and which object supplies this. Write the naive this.constructor.prototype version and watch it recurse. Finish with super in an object literal and in a static method.

Stuck? Three hints, smallest first
  1. 1. A method written with super remembers the object it was defined in. That is what super looks up from.
  2. 2. this is still the receiver, so the parent method reads the caller's data.
  3. 3. this.constructor.prototype is the instance's class, not the parent's. Follow it once and you are back where you started.

The answer

const attemptName = (fn) => { try { fn(); return 'no error'; } catch (err) { return err.name; } };

class Base {
  describe() { return 'base:' + this.till; }
  static origin() { return 'Base'; }
}
class Middle extends Base {
  describe() { return 'middle -> ' + super.describe(); }
  static origin() { return 'Middle of ' + super.origin(); }
}
class Leaf extends Middle {
  describe() { return 'leaf -> ' + super.describe(); }
}

const leaf = new Leaf();
leaf.till = 'Balogun 3';
const wholeChain = leaf.describe();

// The same method, on an object with nothing to do with these classes.
const alien = { till: 'Ikeja 1', describe: Middle.prototype.describe };
const stillReachesBase = alien.describe();
const alienChain = Object.getPrototypeOf(alien) === Object.prototype;

let depth = 0;
class Naive extends Base {
  describe() {
    depth++;
    if (depth > 3) return 'still going round after ' + depth + ' calls';
    return 'naive -> ' + this.constructor.prototype.describe.call(this);
  }
}
const naive = new Naive();
naive.till = 'Yaba';
const recursed = naive.describe();

const parent = { greet() { return 'from the parent'; } };
const child = { __proto__: parent, greet() { return 'child then ' + super.greet(); } };
const literalSuper = child.greet();
const copied = { ...child };
const copiedStillWorks = [Object.getPrototypeOf(copied) === Object.prototype, copied.greet()];

const staticSuper = Middle.origin();

class TooEarly extends Base {
  constructor() { try { this.till = 'Yaba'; } catch (err) { throw err; } }
}
const beforeSuper = attemptName(() => new TooEarly());

console.log('whole chain     :', wholeChain);
console.log('on an alien     :', stillReachesBase);
console.log('naive version   :', recursed, 'after', depth, 'calls');
console.log('literal super   :', literalSuper, '| copied:', copiedStillWorks[1]);

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('each super.describe climbs one link, and this stays the instance', wholeChain, 'leaf -> middle -> base:Balogun 3');
test('the same method on an unrelated object still reaches Base', stillReachesBase, 'middle -> base:Ikeja 1');
test('even though that object\'s prototype is only Object.prototype', alienChain, true);
test('so super comes from where the method was defined, not from this', [leaf.describe.call({ till: 'Ojota' }), depth > 0], ['leaf -> middle -> base:Ojota', true]);
test('the naive this.constructor.prototype version calls itself instead of climbing', recursed, 'naive -> naive -> naive -> still going round after 4 calls');
test('super works in an object literal, against the literal\'s prototype', literalSuper, 'child then from the parent');
test('and survives being copied onto an object with a different prototype', copiedStillWorks, [true, 'child then from the parent']);
test('a static method\'s super is the parent class, not the parent prototype', staticSuper, 'Middle of Base');
test('and touching this before super() in a derived constructor is a ReferenceError', beforeSuper, 'ReferenceError');

Every method written with super carries a hidden reference to the object it was defined in — its home object. super.describe() means "start the lookup at the prototype of my home object", so it depends on where the method was written and not on the receiver at all. The second and third assertions are that stated as strongly as it can be: Middle.prototype.describe, called as a method of a plain object literal, still reaches Base.prototype.describe.

this is the other half, and it does not move: the parent method read Ikeja 1 off the alien object. So super fixes the method to call and leaves the data to the receiver, which is exactly what an override needs.

The fifth assertion is the bug this design prevents. this.constructor.prototype is the prototype of the instance's own class — for a Naive instance that is Naive.prototype, so the "call the parent" line calls itself, and without the depth guard the stack ends. It is the standard failure of hand-rolled inheritance, and it only appears once someone subclasses a class that already used the trick.

Two practical notes. In an object literal, super is available only in shorthand methods — greet() {}, not greet: function () {} — and it follows the literal's prototype, which is why the __proto__: line matters. And in a derived constructor, this does not exist until super() has run: the binding is in a temporal dead zone and touching it is a ReferenceError, because the parent is what creates the object.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.