Javascript 函数不是函数吗?

Javascript 函数不是函数吗?,javascript,angular,typescript,bootstrap-datepicker,Javascript,Angular,Typescript,Bootstrap Datepicker,我有一个小问题,因为我想使用bootstrap datepicker选项中的函数。我有函数检查日期是否在日期数组中: isInArray(array, value) { return !!array.find(item => { return item.getTime() == value.getTime() }); } 我想在这个选项()中使用它,所以我把它放在我的选项中: this.datepickerOptionsForMonths = {

我有一个小问题,因为我想使用bootstrap datepicker选项中的函数。我有函数检查日期是否在日期数组中:

isInArray(array, value) {
    return !!array.find(item => {
        return item.getTime() == value.getTime()
    });
}
我想在这个选项()中使用它,所以我把它放在我的选项中:

this.datepickerOptionsForMonths = {
    minViewMode: 1,
    format: "mm-yyyy",
    startDate: this.dateFrom,
    endDate: this.dateTo,
    beforeShowMonth: function (date: Date) {
       return this.isInArray(this.datepickerRange, date.getDate());
    }
};
现在是问题所在,因为编译已经完成,一切看起来都很好,但在控制台中我发现了错误:

this.isInArray is not a function

可能问题是我已经在DatePickerOptions FormMonts所在的同一个主体中使用了这个函数(在ngOnInit中)。有人能帮我吗?

您正在更改ShowMonth功能之前的
范围

尝试改用箭头函数

beforeShowMonth: (date: Date) => 
  this.isInArray(this.datepickerRange, date.getDate())

箭头函数维护封闭对象的范围。您可以阅读更多有关它的内容

您正在更改ShowMonth
功能之前的
范围

尝试改用箭头函数

beforeShowMonth: (date: Date) => 
  this.isInArray(this.datepickerRange, date.getDate())

箭头函数维护封闭对象的范围。您可以阅读更多关于它的内容

这行
返回项.getTime()==value.getTime()
工作正常吗?item只是一个变量,可能没有getTime方法。另外请注意,您可以使用
Array.prototype.some
而不是
Array.prototype.find
以避免强制转换为布尔值。此行
返回item.getTime()==value.getTime()
有效吗?item只是一个变量,可能没有getTime method?。还请注意,您可以使用
Array.prototype.some
而不是
Array.prototype.find
来避免转换为布尔值。