Is this in scope in the method parameter list in TypeScript?
Consider the following code :
class Foo { constructor(public name) {} bar(str: string = this.name) { console.log(str); } } let f = new Foo("Yo"); f.bar();
The default value of str specified using this , although we are not inside the body of the instance method.
Currently (in typescript 1.8) this works as it translates to:
Foo.prototype.bar = function (str) { if (str === void 0) { str = this.name; } console.log(str); };
So, this used inside the method, but is it listed as legal?
I could not find an answer to this with a cursory glance at the specification .
Note. . This is not legal in C ++ , which makes me wonder if this is a supposed function or just an artifact of the transpilation process.
source share