This project is read-only.

this-itis ?

Topics: General
May 2, 2013 at 3:48 PM
Edited May 2, 2013 at 3:50 PM
I just wrote my first small project using Typescript and I'm definitely impressed. I came from an Actionscript & Javascript background and I think Typescript is mostly offering a best-of-breed solution, and I can't wait for 0.9 to be ready.

One minor thing I noticed was the proliferation of this's necessary for accessing instance variables defined in the class. Am I doing something wrong to require all the 'this' references? Is it by design that only local variables can be accessed without this? If so what is the reason for not inferring class instance scope if no local var exists?

module w.x {
  export class y {

  private v2:string;
   callMe():string { return "x"; }  

   doSomething():void {
     v1 = callMe();  // Wont work, but would in AS3
     v2 = v1 // wont work, but would in AS3
     this.v1 = this.callMe()  // works
     this.v2 = this.v1;  // works
     v3 = "I dont need this";   // works
Maybe its just where I am coming from, but it seems a pain to type out all the this's all over the place when I am almost always referencing instance variables of the enclosing class.
May 2, 2013 at 5:05 PM
Search the forum, you'll find lots of similar threads :) Where devs moving from as3/java/etc get blind-sided by having to type "this", and devs coming from JavaScript are like 'yeah? so?' :) there are several long discussions around it.
May 2, 2013 at 8:55 PM
Ultimately, to comply with the desire to be a superset of ECMAScript 6, `this` cannot be implied in classes. Just get used to using `this` whenever you want to reference instance members inside of a class. :)

Andrew Gaspar

May 2, 2013 at 9:03 PM
Ah yes, that does make sense. It's not a major issue, I was just curious as to the reasoning mostly. So now I will go and harass the ECMAScript 6 folks about it. ;)
Jun 27, 2013 at 9:35 PM
CoffeeScript introduced syntax sugar around this problem:

"@foo" is equivalent to "".
Jun 28, 2013 at 7:10 PM
Edited Jun 28, 2013 at 7:11 PM
Yes, @foo is convenient, and so are a bunch of other things (like "await" support) but we should consider whether TypeScript should deviate too far from the ES6 spec.

CoffeeScript is a new language that is transpiled to JavaScript. Currently, TypeScript is more of a bridge to the future of JavaScript. Do we want it to slowly become CoffeeScript, or something a lot like it? Nope, not in my opinion. I kinda cringe whenever people request features to make TypeScript more like C#.
Jul 20, 2013 at 4:14 AM
blakflag wrote:
Ah yes, that does make sense. It's not a major issue, I was just curious as to the reasoning mostly. So now I will go and harass the ECMAScript 6 folks about it. ;)
Talking to the ES6 folks would be pointless since it won't happen, and shouldn't. Recall that in JavaScript, functions are just objects like anything else. What do you expect this to do:
function foo() { bar = 3; }

function myclass() { = 7;  return this; }

console.log(bar); // this prints 3, yes?

var myobj = new myclass();;
bar = 5;
console.log(; // what should this print?  7 or 3?
console.log(bar); // what about this?  5 or 3?
In order to make functions without this would mean that the function would have to magically change semantics based on which context it was called from. Yuck.