Different TypeScript function notations

In our project we use 2 different notations for defining functions in the Typescript files.In our project we use 2 different notations for defining functions in the Typescript files.One I call the ‘simple notation’ and the other one the ‘lambda notation’ (because it looks like the Lambda notation in C#).

private simpleMethodA(data: NavigationParameters): void { 

    this.doSomething(data);

}
private lambdaMethodA = (data: NavigationParameters): void => {

    this.doSomething(data);

} 

I always wondered what the difference was. It turned out it makes a difference in the JavaScript that is generated:

MenuViewmodel.prototype.simpleMethodA = function (data) {

    this.doSomething(data);

};

 this.lambdaMethodA = function (data) {

    _this.doSomething(data); 

}; 

(in this method, the _this is declared like “var _this = this;” in the viewmodel).

So the simple notation is generated as prototype method and the Lambda method is generated as method on the viewmodel.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s