问题
I am developing a website using HotTowel and TypeScript. In John Papa's excellent PluralSight course, he extended a breezejs entity by creating a constructor and using 'Object.defineProperty' to extend it. For example, he added a property called fullName
as follows.
NB: metadataStore is the breezejs metadata store
function registerPerson(metadataStore) {
metadataStore.registerEntityTypeCtor('Person', Person);
function Person() {
this.isPartial = false;
this.isSpeaker = false;
}
Object.defineProperty(Person.prototype, 'fullName', {
get: function () {
var fn = this.firstName;
var ln = this.lastName;
return ln ? fn + ' ' + ln : fn;
}
});
}
My question is how would I achieve the same using TypeScript ?
回答1:
Typescript support accessor out of the box. The TypeScript code you asked could be :
function registerPerson(metadataStore) {
metadataStore.registerEntityTypeCtor('Person', Person);
}
class Person {
public firstName: string;
public lastName: string;
constructor(public isPartial=false, public isSpeaker=false) {
}
get fullName():string {
return `${this.firstName} ${this.lastName}`;
}
}
来源:https://stackoverflow.com/questions/29041959/extending-a-breeze-entity-using-typescript