inheritance - Angular 2 Typescript inheratance state-pattern issue -
i'm uning typescript inheritance implement state pattern i'm getting runtime error when new superclass. hoping find out why or what's wrong, thanks.
working plunker: https://plnkr.co/edit/xscxr4?p=preview
vehicle.ts import { car } "./car";
export class vehicle { description: string = "generic vehicle"; swap(app: appcomponent): void { app.vehicle = this.iscar(app) ? new vehicle() : new car(); } /* helper demo purposes */ private iscar(app: appcomponent) { return app.vehicle instanceof car; } }
car.ts
import { vehicle } "./vehicle"; export class car extends vehicle { description: string = "car"; }
it's state-pattern. vehicle type 'vehicle' or 'car' can swap instance in 'appcomponent'
app.component.ts
import { component, oninit } '@angular/core'; import { vehicle } "./vehicle"; import { car } "./car"; @component({ moduleid: module.id, selector: 'my-app', templateurl: 'app.component.html', }) export class appcomponent implements oninit { vehicle: vehicle; swap(): void { this.vehicle.swap(this); } ngoninit() { //no problems works //this.vehicle = new car(); //error: typeerror: cannot read property 'prototype' of undefined(…) this.vehicle = new vehicle(); } }
if initialize 'car' instance (subtype) there no problem.
if initialize 'vehicle' instance (supertype) js runtime error
error: typeerror: cannot read property 'prototype' of undefined(…)
i'd know why because seems legitimate thing do.
what doing wrong here?
in plunker, in app.component.ts replace
this.vehicle = new car()
with
this.vehicle = new vehicle()
to see issue.
thanks john
Comments
Post a Comment