代码之家  ›  专栏  ›  技术社区  ›  4xy

TypeScript在类/接口方法中显式显示“this”参数

  •  0
  • 4xy  · 技术社区  · 4 年前

    目前,我使用TypeScript,遇到了使用谷歌无法自行解决的功能(?)。有人能说清楚吗?奇怪的是关于明确 this 类方法的声明。我查看了转译的javascript,看到了第一个参数( )是无效的。所以代码实际上是有效的。但我想知道这是什么,为什么会这样?

    class Foo {
        bar(this: Foo, whatever: any) {
            console.log(whatever);
        } 
    };
    
    ...
    
    const foo = new Foo();
    foo.bar({ baz: 'it works properly' });
    
    0 回复  |  直到 4 年前
    推荐文章