I am learning Angular2
and working with classes
in javascript
first time.
What does the private
parameter and why it couldn't be simply heroService: HeroService
?
constructor(private heroService: HeroService) { }
I am learning Angular2
and working with classes
in javascript
first time.
What does the private
parameter and why it couldn't be simply heroService: HeroService
?
constructor(private heroService: HeroService) { }
Looks like a parameter property (about halfway down the page). Basically, adding an access modifier (public/private/protected/readonly) to a constructor parameter will automatically assign that parameter to a field of the same name.
Specifically, from those docs:
Parameter properties are declared by prefixing a constructor parameter with an accessibility modifier or readonly, or both. Using private for a parameter property declares and initializes a private member; likewise, the same is done for public, protected, and readonly.
So the following are equivalent:
class Foo {
private bar: string;
constructor(bar: string) {
this.bar = bar;
}
}
class Foo {
constructor(private bar: string) {}
}
private will scope this variable for this class (function). public will allow access from outside, if you have the instance of this class. protected is important for properties inside a abstract super class. As i started with Typescript, playground on the TypeScript page (https://www.typescriptlang.org/play/index.html) helped me to understand what really happend. Keep in mind, that TypeScript is sugar for your JavaScript
Understanding private When a member is marked private, it cannot be accessed from outside of its containing class. For example:
class Animal {
private name: string;
constructor(theName: string) { this.name = theName; }
}
new Animal("Cat").name; // Error: 'name' is private;
TypeScript is a structural type system. When we compare two different types, regardless of where they came from, if the types of all members are compatible, then we say the types themselves are compatible.
However, when comparing types that have private and protected members, we treat these types differently. For two types to be considered compatible, if one of them has a private member, then the other must have a private member that originated in the same declaration. The same applies to protected members.
Let’s look at an example to better see how this plays out in practice:
class Animal {
private name: string;
constructor(theName: string) { this.name = theName; }
}
class Rhino extends Animal {
constructor() { super("Rhino"); }
}
class Employee {
private name: string;
constructor(theName: string) { this.name = theName; }
}
let animal = new Animal("Goat");
let rhino = new Rhino();
let employee = new Employee("Bob");
animal = rhino;
animal = employee; // Error: 'Animal' and 'Employee' are not compatible
In this example, we have an Animal
and a Rhino
, with Rhin
o being a subclass of Animal
. We also have a new class Employee
that looks identical to Animal
in terms of shape. We create some instances of these classes and then try to assign them to each other to see what will happen. Because Animal
and Rhino
share the private
side of their shape from the same declaration of private name: string
in Animal
, they are compatible. However, this is not the case for Employee
. When we try to assign from an Employee
to Animal
we get an error that these types are not compatible. Even though Employee
also has a private
member called name
, it’s not the one we declared in Animal
.
For more details check:
https://www.typescriptlang.org/docs/handbook/classes.html