A
A
Alexander Grishin2016-07-20 13:20:35
Angular
Alexander Grishin, 2016-07-20 13:20:35

What is alterEgo?

In the tutorial about heroes there is this

constructor(
      public id: number, 
      public name: string,
      public power: string,
      public alterEgo?: string
    ){/*body*/}

What does the construction: public alterEgo mean?: string

Answer the question

In order to leave comments, you need to log in

2 answer(s)
_
_ _, 2016-07-20
@beerdy

Optional parameter
UPD:
When you declare a public parameter in a constructor, this property is automatically declared on the class, but ? refers to a parameter, not a property. So you can do this

class Hero {
  ...
  public alterEgo?: string;
  constructor(..., _alterEgo?: string) {
    this.alterEgo = _alterEgo; 
  }
}

A
Alexander Grishin, 2016-07-20
@beerdy

Alexander Marchenko Then why if so:

heroes = [
    new Hero(1, 'Windstorm', 'Really Smart',  'optional'),
    new Hero(13, 'Bombasto', 'Super Bad Man' ),
  ];

That works
. And if so:
hero: Hero = {
    id: 1,
    name: 'Windstorm',
    power: 'Black T'
  };

Swears:
app/app.component.ts(32,3): error TS2322: Type '{ id: number; name: string; power: string; }' is not assignable to type 'Hero'.
  Property 'alterEgo' is missing in type '{ id: number; name: string; power: string; }'

PS
And why is this:
Gives an error message:
What kind of construction is this?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question