typescriptで次のコードスニペットは、私が言っているようには機能しません。それは自明である必要があります:
declare interface Date {
toUrlString(): string;
}
Date.prototype.toUrlString = () => {
return this.toISOString().substring(0, 10);
};
document.write(
new Date().toUrlString()
// Error: Object [object Window] has no method 'toISOString'
);
コンパイルされたコードは次のとおりです。
var _this = this;
Date.prototype.toUrlString = function () {
return _this.toISOString().substring(0, 10);
};
document.write(new Date().toUrlString());
どうすればこれを修正できますか?