Inheritance, Public and Private properties

Topics: Language Specification
Oct 17, 2013 at 1:18 PM
I have a suggestion to work with public and private properties and class inheritance.
Suppose we have this Greeter class with ExtraGreeter class that extend Greeter:
class Greeter {
    private privateproperty: string = "";
    public  publicproperty: string = "";
    constructor(message: string) {
        privateproperty = message;
    }
    prototypemethod() {
        return "Hello, " + privateproperty;
    }
    constructordefinedmethod = () => {
        return "Hello, " + privateproperty + this.publicproperty;
    }
}

class ExtraGreeter extends Greeter {
    prototypemethod() {
        return super.prototypemethod();
    }
    constructordefinedmethod = () => {
        return super.constructordefinedmethod();
    }
}
in the typescript 9.1.1 we have this output (excluding compile error):
var __extends = 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 Greeter = (function () {
    function Greeter(message) {
        var _this = this;
        this.privateproperty = "";
        this.publicproperty = "";
        this.constructordefinedmethod = function () {
            return "Hello, " + privateproperty + _this.publicproperty;
        };
        privateproperty = message;
    }
    Greeter.prototype.prototypemethod = function () {
        return "Hello, " + privateproperty;
    };
    return Greeter;
})();

var ExtraGreeter = (function (_super) {
    __extends(ExtraGreeter, _super);
    function ExtraGreeter() {
        _super.apply(this, arguments);
        var _this = this;
        this.constructordefinedmethod = function () {
            return _super.prototype.constructordefinedmethod.call(_this);
        };
    }
    ExtraGreeter.prototype.prototypemethod = function () {
        return _super.prototype.prototypemethod.call(this);
    };
    return ExtraGreeter;
})(Greeter);
I think that the correct output can be this:
var __extends = 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 Greeter = (function () {
    function Greeter(message) {
        var _this = this;
        privateproperty = "";
        this.publicproperty = "";
        this.constructordefinedmethod = function () {
            return "Hello, " + privateproperty + _this.publicproperty;
        };
        privateproperty = message;
    }
    Greeter.prototype.prototypemethod = function () {
        return "Hello, " + privateproperty;
    };
    return Greeter;
})();

var ExtraGreeter = (function (_super) {
    __extends(ExtraGreeter, _super);
    function ExtraGreeter() {
        _super.apply(this, arguments);
        var _this = this;
        var _super_constructordefinedmethod = _this.constructordefinedmethod;
        this.constructordefinedmethod = function () {
            return _super_constructordefinedmethod.call(_this);
            // or: return _super_constructordefinedmethod(); ?
        };
    }
    ExtraGreeter.prototype.prototypemethod = function () {
        return _super.prototype.prototypemethod.call(this);
    };
    return ExtraGreeter;
})(Greeter);
What do you think about?
Thanks!
Gabriele
Oct 17, 2013 at 4:25 PM
The privateproperty in what is labelled the "correct output" is missing a var, which effectively renders it in the global scope.

Once you add the var it's no longer accessible in your prototypemethod.

This issue keeps resurfacing. There is no way to do this.
Oct 17, 2013 at 8:25 PM