我正在编写一个ES6程序,并且从WebStorm的输出中获得非常不正常的行为。有人可以解决这个问题 - 我正在使用Babel进行转换。
class DateLocationValidator{
_appointments;
constructor(appointments){
this._appointments = appointments;
console.log(this._appointments);
}
validate(appointmentViewModel)
{
console.log('validating');
if(this._appointments==null || this._appointments.length==0) {
console.log('appointments null');
console.log(this._appointments);
return {
outcome:true,
reason:'',
conflictId:0
};
}else{
console.log('appointments not null');
var result = this._appointments.where(function(appointment)
{
console.log('searching');
if(appointment.startDate==appointmentViewModel.startDate && appointment.startDate==appointmentViewModel.startDate){
console.log('found');
return true;
}
});
if(result.length>0){
return {
outcome:true,
reason:'There is already an appointment scheduled for this location on this date',
conflictId:result[0].id
};
}
}
}
}
以下是测试:
it("Fails when appointment clashes exactly",function(){
try {
let appointments = [new AppointmentViewModel(1, new Date(2015, 1, 1), new Date(2015, 1, 2))];
let validator = new DateLocationValidator(appointments);
let appointmentViewmodel = new AppointmentViewModel(1, new Date(2015, 1, 1), new Date(2015, 1, 2));
let result = new validator.validate(appointmentViewmodel);
expect(result.outcome).toBe(false);
}
catch(excep){
console.log(excep)
expect(true).toBe(false);
}
});
'appointmentments null'始终输出到控制台。这是在构造函数中正确输出数组之后。
此外,当我尝试从validate调用函数时,我得到一个未定义的错误。
有人可以帮忙吗?
干杯 中号
答案 0 :(得分:4)
出于某种原因,您将validate
称为构造函数。省略new
关键字!
let result = validator.validate(appointmentViewmodel);
// ^^^
在真正的ES6中(未编译),这会引发异常,因为使用方法语法声明的函数无法使用new
调用(它们没有[[construct]]
个插槽)。