Understanding that "this" refers to the instance method is crucial.
Utilizing "this" when calling variables and methods can produce the same results as doing so without it.
Example layout (sample.html):
<p> {{ this.getName() }} </p>
Sample component structure (sample.component.ts):
@Component({
templateUrl: 'sample.html'
})
export class SampleComponent {
public name: string;
constructor() {
this.name = 'John';
}
getName():string {
return this.name;
}
}
Observing the provided code snippets,
both {{ this.getName() }}
and {{ getName() }}
will show the value John
.
Is incorporating "this" recommended for better coding practices?
Are there any concerns related to performance or other factors if it is used?