TypeScript:默认情况下对实例级方法使用 labmdas 是一种好习惯吗?

TypeScript: Is it a good practice to use labmdas by default for instance level methods?

我相信 I understand the difference 以及在 TypeScript classes 中使用 lambda 而不是 'regular' 函数的影响。 要点是 lambda 保留 'this' 并且应该使用它们,特别是如果 class 方法可以 'passed around'、重新赋值等等

作为具有 C# 背景的人,我的问题是: 我是否应该始终默认并更喜欢将 lambda 用于 class 方法? 同样,我希望大多数人会说 'it depends',但我是在最佳实践、安全和合理默认设置的背景下提出这个问题的。

为了便于说明,这里有一个示例 TypeSrcript class 以及它在 ES6 和 ES5 中的转换:

//
// ...following class in TypeScript:
//
export default class AppConfig {
    private _configFilePath: string;    

    public constructor(configFilePath: string) {
         this._configFilePath = configFilePath;
        // do stuff...
    }

    // getter for private instance field
    public get configFilePath(): string { return this._configFilePath; };   

    // override toString using lambda
    public toString = (): string => {
        return `App Config: (config file: "${this._configFilePath}")`;
    }

    // override toString using 'regular' function 
    // the '2' toString2() at the end is just to make it compile and demo 
    public toString2(): string {
        return `App Config: (config file: "${this._configFilePath}")`;
    }   
}


//
// transpiles to following when targeting ES6:
//
class AppConfig {
    constructor(configFilePath) {
        this.toString = () => {
            return `App Config: (config file: "${this._configFilePath}")`;
        };
        this._configFilePath = configFilePath;
    }

    get configFilePath() { return this._configFilePath; };  

    toString2() {
        return `App Config: (config file: "${this._configFilePath}")`;
    }

}

//
// and transpiles to following when targeting ES5:
//
var AppConfig = (function () {
    function AppConfig(configFilePath) {
        var _this = this;
        this.toString = function () {
            return "App Config: (config file: \"" + _this._configFilePath + "\")";
        };
        this._configFilePath = configFilePath;
   }

    Object.defineProperty(AppConfig.prototype, "configFilePath", {
        get: function () { return this._configFilePath; },
        enumerable: true,
        configurable: true
    });
    ;   

    AppConfig.prototype.toString2 = function () {
        return "App Config: (config file: \"" + this._configFilePath + "\")";
    }; 
}());       

一般来说,默认情况下,我只会将箭头函数用于处理程序和其他预期会传递并从其他上下文调用的回调。

箭头函数的问题在于它们不是 class 函数,它们是分配给匿名函数的 class 属性,因此您失去了所有 class 函数特性:重载、覆盖子classes和调用super