是this
范围内TypeScript方法的参数列表中?在打字稿中指定默认值时可以使用`this`吗?
class Foo {
constructor(public name) {}
bar(str: string = this.name) { console.log(str); }
}
let f = new Foo("Yo");
f.bar();
即使我们不是一个实例方法体内的str
默认值是使用this
规定。
目前(以打字稿1.8)这个作品,因为它是transpiled到:
Foo.prototype.bar = function (str) {
if (str === void 0) { str = this.name; }
console.log(str);
};
所以this
用于内的方法,但这种规定是合法的吗?
我无法找到一个答案,这与在specification粗略地看一眼。
注意:这是不合法的C++这使我怀疑它是一个预期的功能,或者只是一个人工翻译过程。
你知道在es6规范的哪里吗? – Alex
[这里](http://www.ecma-international.org/ecma-262/6.0/#sec-function-definitions)中的规格。此外,还给出了一个很好的概述(https://developer.mozilla.org/cs/docs/Web/JavaScript/Reference/Functions/Default_parameters),指出默认参数在通话时进行评估。 –