Assignments to SuperProperty are confusing and may be useless

# ziyunfei (9 years ago)

Today I saw a V8 test case [1] which took me a long time to figure out why it runs like this:

class Test { m() { super.length = 10; // super.length' here has the same effect asthis.length' console.log(super.length); // but super.length' here isTest.prototype.proto.length' (i.e. Object.prototype.length') which still remainsundefined' } } var array = []; Test.prototype.m.call(array); assertEquals(10, array.length);

This syntax is so confusing and I couldn't think of any real use case for it, so should we make `SuperProperty = AssignmentExpression' an early error?

Also, I found an old es-bug thread [2] discussed about this.

[1] chromium.googlesource.com/v8/v8/+/77e30f013a221527bb4ab955188aec44f10fee7f/test/mjsunit/es6/classes-super.js

[2] ecmascript#3246

# Logan Smyth (9 years ago)

Assignment to a super properly definitely has its uses, in the same way that calling a super method does, though definitely less common. Consider an example where a subclass wants to override a parent class's accessor property:

class Parent {
  get prop(){
    return this._prop;
  }
  set prop(val){
    this._prop = val;
  }
}

class Child extends Parent {
  get prop(){
    return super.prop.slice('prefix'.length);
  }
  set prop(val){
    super.prop = 'prefix' + val;
  }
}

Unless the child class wants to (and is able to) duplicate the parent classes logic, or manually trigger the parent getter and setter functions, assigning to the super accessor is the only approach.