Repository navigation
Call base class property via super #4465
Description
Activity
superis referring to the prototype object of a super class, not to the instance object, the instance object is shared between both super and sub classes, hence the limitationIF "super is referring to the prototype object" THEN
"super.getValue()" would not work as do not have instance reference "this".class MyBase { private _value: number; constructor(value: number) { this._value = value; } getValue(): number { return this._value; } get value(): number { return this._value; } } class MyDerived extends MyBase { constructor() { super(2); const f1 = super.getValue(); const f2 = super.value; alert(`${f1} | ${f2} | ${MyBase.prototype.value}`); } } var d = new MyDerived(); var f3 = d.value;alert gets "2 | undefined | undefined". This means super DOES NOT represent prototype, but instance (kind of parent instance, but still instance).
IF "super is referring to the base object instance" THEN
Why "super.value" does not work.IF "super is referring to the prototype object" THEN
"...super is referring to the prototype object of a super class"
class MyBase { getValue(): number { return 1; } get value(): number { return 1; } } class MyDerived extends MyBase { constructor() { super(); const f1 = super.getValue(); const f2 = super.value; } } var d = new MyDerived(); var f3 = d.value;
is compiled to:
var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } __.prototype = b.prototype; d.prototype = new __(); }; var MyBase = (function () { function MyBase() { } MyBase.prototype.getValue = function () { return 1; }; Object.defineProperty(MyBase.prototype, "value", { get: function () { return 1; }, enumerable: true, configurable: true }); return MyBase; })(); var MyDerived = (function (_super) { __extends(MyDerived, _super); function MyDerived() { _super.call(this); var f1 = _super.prototype.getValue.call(this); var f2 = _super.prototype.value; } return MyDerived; })(MyBase); var d = new MyDerived(); var f3 = d.value;
Look over here: live example
missed your point,
valueis a property accessor not just a property, your original complaint looks valid(everything that i said is still valid too :) )
RyanCavanaugh commented
on Aug 26, 2015 MemberMore actionsSee #338
- addedDuplicateAn existing issue was already createdAn existing issue was already created
on Aug 26, 2015 Please do not close this as I'm targeting ES6.
Is there anything in ES2015 that prevents from using super.(base class field/property)?RyanCavanaugh commented
on Aug 26, 2015 MemberMore actionsGood point - we should at least allow this in ES6
- addedBugA bug in TypeScriptA bug in TypeScriptSpecIssues related to the TypeScript language specificationIssues related to the TypeScript language specificationand removedDuplicateAn existing issue was already createdAn existing issue was already created
on Aug 26, 2015 Thanks!
#5860 lifts the restriction for ES6, we still need a separate proposal for the downlevel emit
- addedFixedA PR has been merged for this issueA PR has been merged for this issue
on Dec 11, 2015 var GetPropertyDescriptor = function (o, PropName) { if (o !== null) { return o.hasOwnProperty(PropName) ? Object.getOwnPropertyDescriptor(o, PropName) : GetPropertyDescriptor(Object.getPrototypeOf(o), PropName); } return null; };return GetPropertyDescriptor(base, 'Name').get.call(this); // getter call
GetPropertyDescriptor(base, 'Name').set.call(this, v); // setter callThe above works in plain javascript. It looks really ugly. Typescript would make it look a little nicer, perhaps using the super keyword.
Maybe a bit late, but I was able to access
super.<getter/setter>using bracket notation.super['value'].I wanted to override a getter of my
super, for example:class Base { get value() { return 'a' } } class CustomBase extends Base { get value() { return super['value'] + 'b'; } }
Reacted by James BromwellJuan Sepulveda (@jsep) this is very helpful, do you have any idea why the index operator is allowed but calling
super.valuedirectly is an error?- locked and limited conversation to collaborators
on Jul 31, 2018
In following code:
disallowing the line "const f2 = super.value;" to be valid is... plain stupid.
What is the scenario where this behavior is valid?