Skip to content

Unexpected behaviour #10893

Closed
Closed
@SimonCsoma

Description

@SimonCsoma

TypeScript Version: 1.8.0
Code

class Animal {
    constructor() { 
        this.move()
    }
    move(distanceInMeters: number = 0) {
        console.log(distanceInMeters);
    }
}

class Snake extends Animal {
    public variable = 1;
    constructor() { 
        super();

    }
    move(distanceInMeters = 5) {
        console.log(this.variable);
        super.move(distanceInMeters);
    }
}

Expected behavior:
Class variables should alway be accessible and set and should be compiled like this:

    function Snake() {
        this.variable = 1;
        _super.call(this);
    }

Entire compilation:

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; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Animal = (function () {
    function Animal() {
        this.move();
    }
    Animal.prototype.move = function (distanceInMeters) {
        if (distanceInMeters === void 0) { distanceInMeters = 0; }
        console.log(distanceInMeters);
    };
    return Animal;
}());
var Snake = (function (_super) {
    __extends(Snake, _super);
    function Snake() {
        this.variable = 1;
        _super.call(this);
    }
    Snake.prototype.move = function (distanceInMeters) {
        if (distanceInMeters === void 0) { distanceInMeters = 5; }
        console.log(this.variable);
        _super.prototype.move.call(this, distanceInMeters);
    };
    return Snake;
}(Animal));

Actual behavior:
This causes this.variable in the move function of the snake class to always be undefined when constructing a new class.

    function Snake() {
        _super.call(this);
        this.variable = 1;
    }

Entire compilation:

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; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Animal = (function () {
    function Animal() {
        this.move();
    }
    Animal.prototype.move = function (distanceInMeters) {
        if (distanceInMeters === void 0) { distanceInMeters = 0; }
        console.log(distanceInMeters);
    };
    return Animal;
}());
var Snake = (function (_super) {
    __extends(Snake, _super);
    function Snake() {
        _super.call(this);
        this.variable = 1;
    }
    Snake.prototype.move = function (distanceInMeters) {
        if (distanceInMeters === void 0) { distanceInMeters = 5; }
        console.log(this.variable);
        _super.prototype.move.call(this, distanceInMeters);
    };
    return Snake;
}(Animal));

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions