February 2019
Beginner
694 pages
18h 4m
English
TypeScript also introduces a shorthand version of the constructor function, allowing you to specify parameters with access modifiers directly in the constructor. Consider the following code:
class classWithAutomaticProperties {
constructor(public id: number, private name: string){
}
}
let myAutoClass =
new classWithAutomaticProperties(1, "className");
console.log(`myAutoClass id: ${myAutoClass.id}`);
console.log(`myAutoClass.name: ${myAutoClass.name}`);
This code snippet defines a class named ClassWithAutomaticProperties. The constructor function uses two arguments—an id of type number, and a name of type string. Note, however, the access modifiers of public for id and private for name, defined directly in the ...
Read now
Unlock full access