date: any;
this.date = new Date().toISOString();``
can anyone please help me why do we use this keyword in ts or any other javascript frame work
thanks
date: any;
this.date = new Date().toISOString();``
can anyone please help me why do we use this keyword in ts or any other javascript frame work
thanks
The this
keyword indicates that you are referring to an instance variable in “this” object. E.g., your variable date
is an instance variable of “this” object.
When I teach this concept to students, I tell them that the dot operator means “look inside”. So, this.date
tells the compiler to get “this” object, the dot tells the compiler to look inside this object, and date
is found inside the object, and that is what is getting set.
If this is a new concept to you, you may want to study some more object-oriented languages. In C++ this
is used. In python it is self
. E.g.
thanks a lot
import { Injectable } from '@angular/core';
export class User {
id: number;
name: string;
}
@Injectable({
providedIn: 'root',
})
export class UserService {
constructor() { }
getUsers(): Promise<User[]> {
return Promise.resolve([
{ id: 1, name: 'Maria' },
{ id: 2, name: 'Alex' },
{ id: 3, name: 'Chuntao' },
{ id: 4, name: 'BĂ©atrice' },
{ id: 5, name: 'Sarah' },
{ id: 6, name: 'Andrés' },
{ id: 7, name: 'Abdul' },
{ id: 8, name: 'Pierre' },
{ id: 9, name: 'Jiao' },
{ id: 10, name: 'Seth' }
]);
}
}
```can you explain me use of User class in this code
thanks a lots
You really need to spend some time with some textbooks or online stuff reading about object-oriented programming, objects in javascript, etc.