Javascript 为什么调用注入的服务别名不需要此关键字

Javascript 为什么调用注入的服务别名不需要此关键字,javascript,angular,typescript,dependency-injection,Javascript,Angular,Typescript,Dependency Injection,要在构造函数中调用成员变量和成员方法,我们需要使用这个关键字。但是为什么我们不需要这个关键字来调用注入的服务别名呢 下面的代码是如何工作的,主要是注释代码行 constructor(private appService: AppService) { this.number = appService.getNumber(); // This line is working without using 'this' keyword... console.log(this.number);

要在构造函数中调用成员变量和成员方法,我们需要使用这个关键字。但是为什么我们不需要这个关键字来调用注入的服务别名呢

下面的代码是如何工作的,主要是注释代码行

constructor(private appService: AppService) {
  this.number = appService.getNumber(); // This line is working without using 'this' keyword...
  console.log(this.number);
  this.testMethod();
}
你能解释一下吗。。。为什么我们不需要这个


这只是一个示例,因为您在构造函数中

如果要从其他方法调用它,则需要使用
this
关键字

例如:

testMethod() {
  this.number = this.appService.getNumber(); // This line is working without using 'this' keyword...
  console.log(this.number);
}

勾选此项

简短的回答是:因为它也是构造函数的一个变量。

因为,您可以在类的构造函数中省略
关键字

这个
关键字基本上需要用作构造函数中类的其他方法/函数(但不是必需的/必需的)中变量的引用

比如说-

constructor(private fb : FormBuilder) {
    console.log(fb);   //valid
    console.log(this.fb);    //valid
    this.tempMethod();
  }

  tempMethod() {
    // console.log(fb);   //Invalid
    console.log(this.fb);   //valid
  }

您在构造函数中工作。嗯,当我们试图访问函数中的参数时,通常不使用
this