In a normal class, this is a reference to the class instance
But, on special occasions, this may refer to something else, and the compiler can’t know about it
example:
classPerson{name:string;constructor(name:string){this.name=name;}sayHello(){console.log(`Hello, I'm ${this.name}`);}}constme=newPerson("John",30);me.sayHello();// Hello, I'm JohnconstnewMe={sayHello:me.sayHello};newMe.sayHello();// Hello, I'm undefined (this refers to `newMe` which has no `name` property)
You can pass a special parameter called this to the methods of the Person class, telling the compiler that this is the instance of the Person class.
The compiler will complain if this value is not an instance of the Person class.
You are still able to call the method without passing this
example: hint compiler about this:
classPerson{name:string;constructor(name:string){this.name=name;}sayHello(this:Person){// telling compiler `this` is now an instance of Person classconsole.log(`Hello, I'm ${this.name}`);}}constme=newPerson("John",30);me.sayHello();// Hello, I'm JohnconstnewMe={sayHello:me.sayHello};newMe.sayHello();// compiler will complain, `this` is not an instance of Person class// you are still able to cal the method without passing `this` as `obj.sayHello()`
The shorthand constructor initialization is a shorter way to write the constructor.
You can use the shorthand syntax by specifying the modifier of all constructor parameters.
classPerson{constructor(privateid:string,publicname:string){// nothing else is needed, the Person object will now have and `id` and `name` property coming from the constructor parameters.}}
Setters and getters are used to set and get the value of a property.
Setters and getters are functions, but you don’t need to call them.
classPerson{private_name:string;constructor(name:string){this._name=name;}getname():string{returnthis._name;}setname(str:string){this._name=str;}getmyName():string{// getter, the name of the getter is the name of the propertyreturnthis._name;}setmyName(str:string){// setter, the name of the setter is the name of the propertythis._name=str;}}constme=newPerson("John");console.log(me.name);// John, no need to call getter, it is used as normal propertyme.name="Jane";// no need to call setter, it is used as normal assignment expression// alsoconsole.log(me.myName);// Jane, another way to access the property, using different getterme.myName="Doe";// another setter for the same property _name
private constructors enforce the singleton pattern on a class.
you can not instantiate the class directly, you must use the static method getInstance() to get the instance of the class.
in the getInstance() method, you can use the new keyword to instantiate the class, and call the private constructor.
such a class is called a singleton class, and can have a private static variable that is used to store the instance of the class.
classPerson{privatestaticinstance:Person;privateconstructor(privatename:string){// nothing}staticgetInstance():Person{if(!Person.instance){Person.instance=newPerson("John");}returnPerson.instance;}}constperson=Person.getInstance();// Person instanceconstperson2=Person.getInstance();// the same Person instance as above