Javascript ECMAScript 6类奇怪的行为
我正在写一个ES6程序,从WebStorm的输出中得到了非常不好的行为。有人能澄清这一点吗?我正在使用Babel进行传输Javascript ECMAScript 6类奇怪的行为,javascript,webstorm,ecmascript-6,Javascript,Webstorm,Ecmascript 6,我正在写一个ES6程序,从WebStorm的输出中得到了非常不好的行为。有人能澄清这一点吗?我正在使用Babel进行传输 class DateLocationValidator{ _appointments; constructor(appointments){ this._appointments = appointments; console.log(this._appointments); } validate(appoin
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);
}
});
“约会空值”始终输出到控制台。这是在构造函数中正确输出数组之后
另外,当我试图从validate调用函数时,我得到一个未定义的错误
有人能帮忙吗
干杯
M出于某种原因,您作为构造函数调用了
validate
。省略new
关键字
let result = validator.validate(appointmentViewmodel);
// ^^^
在真正的ES6(未传输)中,这会引发异常,因为使用方法语法声明的函数不能用
new
(它们没有[[construct]]
插槽)调用。出于某种原因,您将调用validate
作为构造函数。省略new
关键字
let result = validator.validate(appointmentViewmodel);
// ^^^
在真正的ES6(未传输)中,这会引发异常,因为使用方法语法声明的函数不能用
new
调用(它们没有[[construct]]
槽)。Doh-这完全是一个打字错误!谢谢哦-那完全是个打字错误!谢谢