Skip to content

Missing this reference for function declaration instead of fat arrow functions #13842

@CSchulz

Description

@CSchulz

TypeScript Version: 2.0.10 / 2.0.2 / 2.1.5

Code
http://plnkr.co/edit/ZQONYB5oaLhO8aVRaLjE?p=preview

    this.service.getData().subscribe(
      function(x) {
        this.data = x;
      });

    this.service.getData().subscribe(
      (x) => {
         this.data = x;
      });
// transpiled output
    App.prototype.ngOnInit = function () {
        this.service.getData().subscribe(function (x) {
            this.data = x;
        });
    };

    App.prototype.ngOnInit = function () {
        var _this = this;
        this.service.getData().subscribe(function (x) {
            _this.data = x;
        });
    };

Expected behavior:
It should make no difference between function or fat arrow function.

Actual behavior:
Missing this reference. var _this = this; for the function

// transpiled output
    App.prototype.ngOnInit = function () {
        var _this = this;
        this.service.getData().subscribe(function (x) {
            _this.data = x;
        });
    };

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions