对typescript中的对象调用方法

对typescript中的对象调用方法,typescript,Typescript,我在一个类中有这个方法,当调用这个方法时,它调用一个函数validations[dateFormat]()基于参数dateFormat public isValidYearMonthDate(userDateArr: any, dateFormat: string) { const dateFormatArr = dateFormat.split('-'); const validations: any = { 'YYYY-MM-DD': this.checkYe

我在一个类中有这个方法,当调用这个方法时,它调用一个函数
validations[dateFormat]()
基于参数
dateFormat

public isValidYearMonthDate(userDateArr: any, dateFormat: string) {
    const dateFormatArr = dateFormat.split('-');
    const validations: any = {
        'YYYY-MM-DD': this.checkYearMonthDate,
        'MM_DD_YYYY': this.checkMonthDateYear,
        'DD_MM_YYYY': this.checkDateMonthYear
    };
    return validations[dateFormat](dateFormatArr, userDateArr);
}
假设调用了下面的函数,我对
this
关键字有疑问。它指向第2行的
validations
对象this.isMonthValid而不是类

private checkYearMonthDate(dateArr: string[], arr: any) {
    return this.isMonthValid(arr[2], arr[1], arr[0]); // LINE 2
}
我尝试了
\u this=this
,但运气不好

尝试用下面的方法调用函数

return validations[dateFormat].bind(this)(dateFormatArr, userDateArr);
bind()方法创建一个新函数,在调用该函数时,将其this关键字设置为提供的值,并在调用新函数时提供的任何参数之前提供给定的参数序列


实际上,我通过使用apply修复了它:

validations[dateFormat].apply(this, [dateFormatArr, userDateArr]);