Matej
Matej

Reputation: 147

Understanding of code part in AngularJS/Ionic2

I'm currently trying to understand a semi-finished Ionic 2 Project.

I found this decleration:

login: {username?: string, password?: string} = {};

For me, it looks like an object "login", with the properties username and passwort. What's the meaning of the question marks behind the properties and what sense has this allocation with {}?

Thanks in advance

Upvotes: 0

Views: 41

Answers (1)

Ivar Reukers
Ivar Reukers

Reputation: 7727

It defines a variable called login

The part after the : is for defining the structure of login, it has 2 properties, username and password. Both are optional (the ? - so could be null or undefined)

The { } is initializing the login variable with an undefined username and undefined password (Probably so he can use it later on as login.username and login.password in an ngModel for example)

If he wasn't using the ? it would result in an error Property 'username' is missing in type '{}' which would force him to rewrite his initialization to be

login: {username: string, password: string} = { username: '', password: ''};

Upvotes: 2

Related Questions