プロパティのためのフィールドをバッキングする代わりに使用する汎用メソッドのセットを作成します。私はセッターでいくつかの一般的なことをする必要があります(Angular 5のイベントエミッタを呼び出します)、毎回それを書きたくありません。TypeScriptの呼び出しプロパティの名前を取得
は、私は文字列としてプロパティ名を渡すことができますが、私は ことを避けたい:
私はこれらの3つのことを試してみました。
私はError.stackでも動作しようとしましたが、スタック トレースの解析はさまざまなブラウザで異なり、私はそれを気に入らないでしょう。
私が
arguments.callee.caller
を使用しようとすると、'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
が得られます。
その他の提案はありますか?
以下の使用例。コメントは// somehow get calling property name
です。この例では、"myProperty"
文字列を返すコードがあります。それは確実に生産のような場合には、プロパティ名を取得することは不可能ですので、
class ApplicationComponent {
private readonly properties = new Map<string, any>();
protected getProperty<T>(): T {
const propertyName = // somehow get calling property name
return <T>this.properties[propertyName];
}
protected setProperty<T>(value: T): void {
const propertyName = // somehow get calling property name
const oldValue = <T>this.properties[propertyName];
if (oldValue === value) {
return;
}
this.properties[propertyName] = value;
const eventEmitter = this[`${propertyName}Change`];
if (eventEmitter != null && eventEmitter instanceof EventEmitter) {
eventEmitter.emit(value);
}
}
}
class SomeComponent extends ApplicationComponent {
get myProperty(): SomeType {
return this.getProperty();
}
set myProperty(value: SomeType) {
this.setProperty(value);
}
readonly myPropertyChange = new EventEmitter<SomeType>();
}
、任意のプロパティを傍受するES6プロキシを見て、動的に呼び出す:https://developer.mozilla.org/en-US/docs/ Web/JavaScript/Reference/Global_Objects/Proxy TypeScript側では、マップされた型と 'keyof'演算子を見てください:https://blog.mariusschulz.com/2017/01/20/typescript-2-1-mapped -タイプ –