December 2017
Beginner
372 pages
10h 32m
English
Accessors are getter and setter functions that give us more control over how property values can be accessed. They have their own special syntax. In the following example, we have accessors for a the title property. You define them by using the get and set keywords. On a high level, accessors are just functions, but they both have the same name:
class Book { private _title:string; get title():string{ return this._title; } set title(value:string){ if(value != ""){ this._title = value; } }}let typeScript = new Book();typeScript.title = "TypeScript By Example";
The getter will always contain an empty set of parentheses after the name. The setter must be passed exactly one parameter, and you are not allowed to specify a return type ...
Read now
Unlock full access