我并不是说这就是我的意思

ora*_*rad 3 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'
);
Run Code Online (Sandbox Code Playgroud)

编译代码是:

var _this = this;
Date.prototype.toUrlString = function () {
    return _this.toISOString().substring(0, 10);
};
document.write(new Date().toUrlString());
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

Joh*_*yHK 5

=>"胖箭头"符号调用词法作用域规则.如果您不想要,请使用传统功能:

Date.prototype.toUrlString = function() {
    return this.toISOString().substring(0, 10);
};
Run Code Online (Sandbox Code Playgroud)