February 2019
Beginner
694 pages
18h 4m
English
ECMAScript 5 introduces the concept of property accessors. An accessor is simply a function that is called when a user of our class either sets a property, or retrieves a property. This means that we can detect when users of our class are either getting or setting a property, and can be used as a trigger mechanism for other logic. To use accessors, we create a pair of get and set functions (with the same function name) in order to access an internal property. This concept is best understood with some code samples, as follows:
class ClassWithAccessors { private _id : number | undefined; get id() { console.log(`inside get id()`); return <number>this._id; } set id(value:number) { console.log(`inside set id()`); this._id ...Read now
Unlock full access