Complete the code to declare a class with a constructor that uses parameter properties shorthand to create a public property.
class Person { constructor(public [1]: string) {} } const p = new Person("Alice"); console.log(p.name);
public before the parameter.The public keyword before the constructor parameter name creates a public property automatically.
Complete the code to create a private property using parameter properties shorthand.
class Car { constructor(private [1]: number) {} getSpeed() { return this.speed; } } const car = new Car(100); console.log(car.getSpeed());
The private keyword before the parameter speed creates a private property accessible inside the class.
Fix the error in the constructor by using parameter properties shorthand correctly.
class Book { title: string; constructor([1]: string) { this.title = title; } } const b = new Book("My Book"); console.log(b.title);
Adding public before the parameter title in the constructor automatically creates and initializes the property.
Fill both blanks to create a readonly property using parameter properties shorthand and access it in a method.
class User { constructor([1] [2]: string) {} getUsername() { return this.username; } } const user = new User("john_doe"); console.log(user.getUsername());
private instead of readonly.Using readonly and the parameter name username creates a readonly property accessible in the class.
Fill all three blanks to create a protected property with parameter properties shorthand, initialize it, and access it in a subclass.
class Animal { constructor([1] [2]: string) {} } class Dog extends Animal { getType() { return this.[3]; } } const dog = new Dog("mammal"); console.log(dog.getType());
private which blocks subclass access.The protected keyword creates a property accessible in subclasses. The property name type is used in both constructor and subclass method.