javascript - Provide default value to getter without causing stack overflow in Angular 2 Model Class -


so i'm trying find way provide fallback behavior getter on class in javascript. idea provide modified version of property (title) if property being accessed set null @ creation time. trouble i'm facing recursive call when getting subtitle property because it's accessing itself. rename _subtitle, typescript you'd have modify interface , provide throwaway temporary value _subtitle in addition subtitle, breaks semantics of interface.

export interface ifoo {   title: string;   subtitle?: string; }  export class foo implements ifoo {   public title: string;   public set subtitle(val) {      this.subtitle = val;   }   public subtitle() {     return this.subtitle ? this.subtitle : this.title.split('//')[0];   };    constructor(obj?: any) {     this.title = obj && obj.title || null; } 

so ended realizing shortly after can use private property serve 'temporary' storage location without breaking semantics or contract of interface. so, few simple changes, setter/getter working!

  //...   //    private _subtitle: string;   public set subtitle(val) {     this._subtitle = val;   }   public subtitle() {     return this._subtitle ? this._subtitle : this.title.split('//')[1].trim();   };   //... 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -