proposal: use another keyword for "this" as "this object" reference

Topics: Language Specification
Nov 20, 2013 at 8:44 AM
First of all, i like the overall specification of typescript, you guys did an amazing job!
But although i would not call my self an javascript beginner, i felt that there is something wrong with the handling of the keyword "this".

I think it is a bad design, if the same keyword has different meanings depending on the context. It is is neither transparent [1] nor practical [2]. It is not nonambiguous!

Would it be more sophisticated, if there would be another keyword for "this object". For example "self". The meaning would be transparent and you can use both "this" and "self" in one function body (without introducing a "self" variable outside the function/lambda.

[1] by the means of looking at the function body
[2] sometimes you need "this" to be the references to the current function AND a reference to the class around it. so basically you need to introduce a variable outside the function to "this"
Developer
Nov 21, 2013 at 12:07 AM
I think it is a bad design, if the same keyword has different meanings depending on the context. It is is neither transparent [1] nor practical [2]. It is not nonambiguous!
The problem is that TypeScript needs to correctly model existing JavaScript code, and the semantics you're describing for 'this' are just those defined by JavaScript (arrow functions being a part of the upcoming ES6). Arrow functions are meant to help model some common patterns with 'this' contexts changing while you can manually bind 'this' in the scope you need and capture that value where needed.

There are some additional features that could help improve the situation here but it will always need to be compatible with existing JavaScript and TypeScript.
Marked as answer by humbrie on 11/21/2013 at 1:48 AM
Nov 21, 2013 at 9:54 AM
OK, thank you. I didn´t check on the new features of ES6 and i thought, the "this" implementation is a typescript feature.
Although i think its the right way to be compatible to ES6, personally i dont like the ambiguous design of "this" feature.

Thank you very much for TypeScript, i´m looking forward to the next version of the visual studio extension.