July 2018
Intermediate to advanced
178 pages
3h 52m
English
In TypeScript, you can specify optional arguments with the ? operator. While this feature is good and I will use it without moderation in the coming chapters, it opens the door to the following ugliness:
class User{
private name:string;
public getSetName(name?:string):any{
if(name !== undefined){
this.name = name;
}else{
return this.name
}
}
}
Here, we test whether the optional name argument was passed with !== undefined. If the getSetName function received something, it'll act as a setter, otherwise, as a getter. The fact that the function doesn't return anything when used as a setter is authorized by any return type.
For clarity and readability, stick to the ActionScript-inspired getter and setter:
class User{private ...Read now
Unlock full access